作业 03- 代码重构
发布于: 2020 年 06 月 24 日
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
![](https://static001.geekbang.org/infoq/73/730592aa2521c2100daa584961f15337.png)
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
![](https://static001.geekbang.org/resource/image/a1/a6/a1eedc476960bd74eaa141939adc88a6.png)
public class GenerateWindow { public static void main(String[] args){ WinFrom winFrom = new WinFrom("WINDOW窗口"); winFrom.add(new Picture("LOGO图片")); winFrom.add(new Button("登陆")); winFrom.add(new Button("注册")); Frame frame1 = new Frame("FRAME1"); frame1.add(new Lable("用户名")); frame1.add(new TextBox("输入框")); frame1.add(new Lable("密码")); frame1.add(new PasswordBox("密码框")); frame1.add(new CheckBox("复选框")); frame1.add(new TextBox("记住密码")); frame1.add(new LinkLable("忘记密码")); winFrom.add(frame1); winFrom.print(); }}public abstract class Component { protected String name = null; protected List<Component> list = new ArrayList<>(); public Component() { } public Component(String name) { this.name = name; } public abstract void print(); public abstract void add(Component component);}public class WinFrom extends Component { public WinFrom(String name){ super(name); } public void print() { list.forEach(it->it.print()); } @Override public void add(Component component) { list.add(component); }}public class Frame extends Component { public Frame(String name){ super(name); } @Override public void print() { list.forEach(it->it.print()); } @Override public void add(Component component) { list.add(component); }}public class Button extends Component { public Button(String name){ super(name); } @Override public void print() { System.out.println(name); } @Override public void add(Component component) { }}public class CheckBox extends Component { public CheckBox(String name){ super(name); } @Override public void print() { System.out.println(name); } @Override public void add(Component component) { }}public class Lable extends Component { public Lable(String name){ super(name); } @Override public void print() { System.out.println(name); } @Override public void add(Component component) { }}public class LinkLable extends Component { public LinkLable(String name){ super(name); } @Override public void print() { System.out.println(name); } @Override public void add(Component component) { }}public class PasswordBox extends Component { public PasswordBox(String name){ super(name); } @Override public void print() { System.out.println(name); } @Override public void add(Component component) { }}public class Picture extends Component { public Picture(String name){ super(name); } @Override public void print() { System.out.println(name); } @Override public void add(Component component) { }}
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 45
![用户头像](http://static001.geekbang.org/account/avatar/00/14/61/6d/42e6f255.jpg?x-oss-process=image/resize,w_200,h_200)
梦子说
关注
还未添加个人签名 2018.12.01 加入
还未添加个人简介
评论