架构师训练营 第三周 命题作业
发布于: 2020 年 06 月 25 日
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
public interface Component { List<Component> getChilds(); void add(Component component); void setName(String name); String getName(); void print();}
public abstract class AbComponent implements Component { private List<Component> childs = new ArrayList<>(); private String name; @Override public List<Component> getChilds() { return childs; } @Override public void add(Component component) { childs.add(component); } @Override public void setName(String name) { this.name = name; } @Override public String getName() { return name; }}
public class Button extends AbComponent{ public Button(String name) { setName(name); } @Override public void print() { System.out.println("print Button(" + this.getName() + ")"); }}
public class Checkbox extends AbComponent { public Checkbox(String name){ setName(name); } @Override public void print() { System.out.println("print Checkbox(" + this.getName() + ")"); }}
public class Frame extends AbComponent { public Frame(String name) { setName(name); } @Override public void print() { System.out.println("print Picture(" + this.getName() + ")"); getChilds().stream().forEach(component -> { component.print(); }); }}
public class Lable extends AbComponent { public Lable(String name){ setName(name); } @Override public void print() { System.out.println("print Lable(" + this.getName() + ")"); }}
public class LinkLable extends AbComponent { public LinkLable(String name) { setName(name); } @Override public void print() { System.out.println("print LinkLable(" + this.getName() + ")"); }}
public class PasswordBox extends AbComponent { public PasswordBox(String name) { setName(name); } @Override public void print() { System.out.println("print PasswordBox(" + this.getName() + ")"); }}
public class Picture extends AbComponent{ public Picture(String name){ setName(name); } @Override public void print() { System.out.println("print Picture(" + this.getName() + ")"); }}
public class TextBox extends AbComponent { public TextBox(String name){ setName(name); } @Override public void print() { System.out.println("print TextBox(" + this.getName() + ")"); }}
public class WinForm extends AbComponent{ public WinForm(String name) { setName(name); } @Override public void print() { System.out.println("print WinForm(" + this.getName() + ")"); getChilds().stream().forEach(component -> { component.print(); }); }}
public class Test { public static void main(String[] args) { Component winForm = new WinForm("Windows窗口"); winForm.add(new Picture("LOGO图片")); winForm.add(new Button("登录")); winForm.add(new Button("注册")); Component frameComponent = new Frame("FRAME1"); winForm.add(frameComponent); frameComponent.add(new Lable("用户名")); frameComponent.add(new TextBox("文本框")); frameComponent.add(new Lable("密码")); frameComponent.add(new PasswordBox("密码框")); frameComponent.add(new Checkbox("复选框")); frameComponent.add(new TextBox("记住用户名")); frameComponent.add(new LinkLable("忘记密码")); winForm.print(); }}
输出:
print WinForm(Windows窗口)
print Picture(LOGO图片)
print Button(登录)
print Button(注册)
print Picture(FRAME1)
print Lable(用户名)
print TextBox(文本框)
print Lable(密码)
print PasswordBox(密码框)
print Checkbox(复选框)
print TextBox(记住用户名)
print LinkLable(忘记密码)
Process finished with exit code 0
划线
评论
复制
发布于: 2020 年 06 月 25 日 阅读数: 34
RZC
关注
还未添加个人签名 2019.07.25 加入
还未添加个人简介
评论