架构师训练营 1 期 -- 第三周作业
发布于: 2020 年 10 月 04 日
作业一:
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
为了便于阅读,所有代码写在Lesson3.java类中,代码如下:
import java.util.ArrayList;import java.util.List;public class Lesson3 { public static void main(String[] args) { WinForm winForm = new WinForm("WINDOW窗口"); winForm.addComponent(new Picture("LOGO图片")); winForm.addComponent(new Button("登录")); winForm.addComponent(new Button("注册")); Frame frame = new Frame("FRAME1"); frame.addComponent(new Label("用户名")); frame.addComponent(new TextBox("文本框")); frame.addComponent(new Label("密码")); frame.addComponent(new PasswordBox("密码框")); frame.addComponent(new CheckBox("复选框")); frame.addComponent(new TextBox("记住用户名")); frame.addComponent(new LinkLabel("忘记密码")); winForm.addComponent(frame); winForm.draw(); } interface Component { void draw(); } /**** * 创建一个BaseComponent类,定义name字段 */ static abstract class BaseComponent implements Component { protected String name; public BaseComponent(String name) { this.name = name; } } /***** * Container继承自BaseComponent,它也实现了Component */ static abstract class Container extends BaseComponent { // 存储Component列表 protected List<Component> componentList = new ArrayList<>(); public Container(String name) { super(name); } public void addComponent(Component item) { componentList.add(item); } /**** * 遍历组件列表,并调用它的draw方法 */ protected void doDraw() { for (Component item : componentList) { item.draw(); } } } static class Picture extends BaseComponent { public Picture(String name) { super(name); } @Override public void draw() { System.out.println("print Picture(" + name + ")"); } } static class Button extends BaseComponent { public Button(String name) { super(name); } @Override public void draw() { System.out.println("print Button(" + name + ")"); } } static class Label extends BaseComponent { public Label(String name) { super(name); } @Override public void draw() { System.out.println("print Label(" + name + ")"); } } static class CheckBox extends BaseComponent { public CheckBox(String name) { super(name); } @Override public void draw() { System.out.println("print CheckBox(" + name + ")"); } } static class TextBox extends BaseComponent { public TextBox(String name) { super(name); } @Override public void draw() { System.out.println("print TextBox(" + name + ")"); } } static class LinkLabel extends BaseComponent { public LinkLabel(String name) { super(name); } @Override public void draw() { System.out.println("print LinkLabel(" + name + ")"); } } static class PasswordBox extends BaseComponent { public PasswordBox(String name) { super(name); } @Override public void draw() { System.out.println("print PasswordBox(" + name + ")"); } } static class WinForm extends Container { public WinForm(String name) { super(name); } @Override public void draw() { System.out.println("print WinForm(" + name + ")"); doDraw(); } } static class Frame extends Container { public Frame(String name) { super(name); } @Override public void draw() { System.out.println("print Frame(" + name + ")"); doDraw(); } }}
设计思路可以参考代码注释,运行结果如下:
划线
评论
复制
发布于: 2020 年 10 月 04 日 阅读数: 37
曾彪彪
关注
还未添加个人签名 2019.03.23 加入
还未添加个人简介
评论