架构师训练营 W03 作业
发布于: 2020 年 11 月 05 日
作业一:请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
作业二:请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
public interface Component { void print();}
public class BaseComponent implements Component{ private String name; public BaseComponent(String name) { this.name = name; } @Override public void print() { System.out.println("print " + this.getClass().getSimpleName() + "(" + this.name + ")"); }}
public class ComponentContainer extends BaseComponent { private final List<Component> children = new ArrayList<>(); public ComponentContainer(String name) { super(name); } public void addChild(Component Component) { children.add(Component); } @Override public void print() { super.print(); for (Component child : children) { child.print(); } }}
public class WinForm extends ComponentContainer { public WinForm(String name) { super(name); }}
public class Button extends BaseComponent{ public Button(String name) { super(name); }}
public class Picture extends BaseComponent{ public Picture(String name) { super(name); }}
public class Frame extends ComponentContainer { public Frame(String name) { super(name); }}
public class Label extends BaseComponent{ public Label(String name) { super(name); }}
public class TextBox extends BaseComponent{ public TextBox(String name) { super(name); }}
public class PasswordBox extends BaseComponent{ public PasswordBox(String name) { super(name); }}
public class CheckBox extends BaseComponent{ public CheckBox(String name) { super(name); }}
public class LinkLabel extends BaseComponent{ public LinkLabel(String name) { super(name); }}
public class Test { public static void main(String[] args) { WinForm form = new WinForm("WINDOW窗口"); form.addChild(new Picture("LOGO图片")); form.addChild(new Button("登陆")); form.addChild(new Button("注册")); Frame frame = new Frame("FRAME1"); frame.addChild(new Label("用户名")); frame.addChild(new TextBox("文本框")); frame.addChild(new Label("密码")); frame.addChild(new PasswordBox("密码框")); frame.addChild(new CheckBox("复选框")); frame.addChild(new TextBox("记住用户名")); frame.addChild(new LinkLabel("忘记密码")); form.addChild(frame); form.print(); }}
划线
评论
复制
发布于: 2020 年 11 月 05 日阅读数: 22
版权声明: 本文为 InfoQ 作者【Geek_f06ede】的原创文章。
原文链接:【http://xie.infoq.cn/article/048b06a7ea1b86938c034527f】。未经作者许可,禁止转载。
Geek_f06ede
关注
还未添加个人签名 2019.12.09 加入
还未添加个人简介
评论