架构师训练营第三周作业
发布于: 2020 年 06 月 25 日
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
定义 Compont 接口
public interface Component { void print();}
定义各组件类
public class Window implements Component { private String text; List<Component> componentList = new ArrayList<>(); public Window(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint Window " + text); componentList.forEach(Component::print); } public void addCompont(Component compont){ componentList.add(compont); }}public class Button implements Component { private String text; public Button(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint Button " + text); }}public class CheckBox implements Component { private String text; public CheckBox(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint CheckBox " + text); }}public class Frame implements Component { private String text; List<Component> componentList = new ArrayList<>(); public Frame(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint Frame " + text); componentList.forEach(Component::print); } public void addCompont(Component compont){ componentList.add(compont); }}public class Frame implements Component { private String text; List<Component> componentList = new ArrayList<>(); public Frame(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint Frame " + text); componentList.forEach(Component::print); } public void addCompont(Component compont){ componentList.add(compont); }}public class LinkLable implements Component { private String text; public LinkLable(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint LinkLable " + text); }}public class PasswordBox implements Component { private String text; public PasswordBox(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint PasswordBox " + text); }}public class Picture implements Component { private String text; public Picture(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint Picture" + text); }}public class TextBox implements Component { private String text; public TextBox(String text){ this.text = text; } @Override public void print() { System.out.print("\nprint TextBox " + text); }}
测试类验证结果
public class Test { public static void main(String[] args){ Window window = new Window("WINDOW窗口"); window.addCompont(new Button("登录")); window.addCompont(new Picture("LOGO图片")); window.addCompont(new Button("登录")); window.addCompont(new Button("注册")); Frame frame = new Frame("FRAME1"); frame.addCompont(new Lable("用户名")); frame.addCompont(new TextBox("文本框")); frame.addCompont(new Lable("密码")); frame.addCompont(new PasswordBox("密码框")); frame.addCompont(new CheckBox("复选框")); frame.addCompont(new TextBox("记住用户名")); frame.addCompont(new LinkLable("忘记密码")); window.addCompont(frame); window.print(); }}
输出结果
print Window WINDOW窗口print Button 登录print PictureLOGO图片print Button 登录print Button 注册print Frame FRAME1print Lable 用户名print TextBox 文本框print Lable 密码print PasswordBox 密码框print CheckBox 复选框print TextBox 记住用户名print LinkLable 忘记密码
划线
评论
复制
发布于: 2020 年 06 月 25 日 阅读数: 26
斌
关注
还未添加个人签名 2018.05.19 加入
还未添加个人简介
评论