写点什么

架构师训练营第三周作业(9.28-10.4)

用户头像
zjzj2017
关注
发布于: 2020 年 10 月 08 日

作业 1:

  1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。


  1. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。



package com.company;

import java.util.ArrayList;import java.util.List;
interface Componet { void print();}
class WinForm implements Componet { private List<Componet> subNodes = new ArrayList<>(); String name; public WinForm(String name) { this.name = name; }
public void addSubNode(Componet subnode) { subNodes.add(subnode); }
@Override public void print() { System.out.println("print WinForm" + "(" + this.name + ")"); for (Componet node: subNodes) { node.print(); } }}
class Picture implements Componet { String name; public Picture(String name) { this.name = name; }
@Override public void print() { System.out.println("print Picture" + "(" + this.name + ")"); }}
class Button implements Componet { String name; public Button(String name) { this.name = name; }
@Override public void print() { System.out.println("print Button" + "(" + this.name + ")"); }}
class Frame implements Componet { String name; private List<Componet> subNodes = new ArrayList<>();
public Frame(String name) { this.name = name; }
@Override public void print() { System.out.println("print Frame" + "(" + this.name + ")"); for (Componet node : subNodes) { node.print(); } }
public void addSubNode(Componet subnode) { subNodes.add(subnode); }}
class Lable implements Componet { String name; public Lable(String name) { this.name = name; }
@Override public void print() { System.out.println("print Lable" + "(" + this.name + ")" ); }}
class TextBox implements Componet { String name; public TextBox(String name) { this.name = name; }
@Override public void print() { System.out.println("print TextBox" + "(" + this.name + ")"); }}
class PasswordBox implements Componet { String name; public PasswordBox(String name) { this.name = name; }
@Override public void print() { System.out.println("print PasswordBox" + "(" + this.name + ")"); }}
class CheckBox implements Componet { String name; public CheckBox(String name) { this.name = name; }
@Override public void print() { System.out.println("print CheckBox" + "(" + this.name + ")"); }}
class LinkLable implements Componet { String name; public LinkLable(String name) { this.name = name; }
@Override public void print() { System.out.println("print LinkLable" + "(" + this.name + ")"); }}

public class Main { public static void main(String[] args) { // write your code here WinForm winForm = new WinForm("WINDOW窗口"); winForm.addSubNode(new Picture("LOGO图片")); winForm.addSubNode(new Button("登录")); winForm.addSubNode(new Button("注册"));
Frame frame = new Frame("FRAME1"); frame.addSubNode(new Lable("用户名")); frame.addSubNode(new TextBox("文本框")); frame.addSubNode(new Lable("密码")); frame.addSubNode(new PasswordBox("密码框")); frame.addSubNode(new CheckBox("复选框")); frame.addSubNode(new TextBox("记住用户名")); frame.addSubNode(new LinkLable("忘记密码"));
winForm.addSubNode(frame);
winForm.print(); }}
复制代码


用户头像

zjzj2017

关注

还未添加个人签名 2017.11.23 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第三周作业(9.28-10.4)