<<架构师训练营>> 第三周作业一
发布于: 2020 年 06 月 24 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
作业结果设计包含类:
Button 按钮类
CheckBox 单选框类
Form 窗体类
Frame 窗体架构类
Label 标签类
LinkLabel 链接标签类
PasswordBox 密码框类
PictureBox 图片框类
TextBox 文本框类
Window 窗体接口
Container 窗体容器接口
类图:
因为类图中元素过多, 只画出了部分类的关系, 剩下没在图中出现的类均实现 Window 接口, 结构和 Label 类类似。
Window 接口代码
public interface Window { void draw();}
Container 接口代码
public interface Container { void addWindow(Window window);}
Form 类代码
public class Form implements Window, Container { private final List<Window> childWindows; private final String name; public Form(String name) { this.childWindows = new ArrayList<>(); this.name = name; } @Override public void draw() { System.out.println(String.format("Print WinForm(%s)", name)); childWindows.forEach(Window::draw); } @Override public void addWindow(Window window) { this.childWindows.add(window); }}
Frame 类代码
public class Frame implements Window, Container { final private String name; final private List<Window> childWindows; public Frame(String name) { this.name = name; this.childWindows = new ArrayList<>(); } @Override public void draw() { System.out.println(String.format("print Frame(%s)", name)); this.childWindows.forEach(Window::draw); } @Override public void addWindow(Window window) { this.childWindows.add(window); }}
Button 类代码
public class Button implements Window { final private String name; public Button(String name) { this.name = name; } @Override public void draw() { System.out.println(String.format("print Button(%s)", name)); }}
CheckBox 类代码
public class CheckBox implements Window { final String name; public CheckBox(String name) { this.name = name; } @Override public void draw() { System.out.println(String.format("print CheckBox(%s)", name)); }}
Label 类代码
public class Label implements Window { final String name; public Label(String name) { this.name = name; } @Override public void draw() { System.out.println(String.format("print Label(%s)", name)); }}
LinkLabel 类代码
public class LinkLabel implements Window { final String name; public LinkLabel(String name) { this.name = name; } @Override public void draw() { System.out.println(String.format("print LinkLabel(%s)", name)); }}
Password 类代码
public class PasswordBox implements Window { final String name; public PasswordBox(String name) { this.name = name; } @Override public void draw() { System.out.println(String.format("print PasswordBox(%s)", name)); }}
PictureBox 类代码
public class PictureBox implements Window { final private String name; public PictureBox(String name) { this.name = name; } @Override public void draw() { System.out.println(String.format("print Picture(%s)", name)); }}
TextBox 类代码
public class TextBox implements Window { final private String name; public TextBox(String name) { this.name = name; } @Override public void draw() { System.out.println(String.format("print TextBox(%s)", name)); }}
执行入口类代码
public class WinForm { public static void main(String[] args) { Form winForm = new Form("WINDOW 窗口"); winForm.addWindow(new PictureBox("LOGO 图片")); winForm.addWindow(new Button("登录")); winForm.addWindow(new Button("注册")); Frame frame = new Frame("FRAME1"); frame.addWindow(new Label("用户名")); frame.addWindow(new TextBox("文本框")); frame.addWindow(new Label("密码")); frame.addWindow(new PasswordBox("密码框")); frame.addWindow(new CheckBox("复选框")); frame.addWindow(new TextBox("记住用户名")); frame.addWindow(new LinkLabel("忘记密码")); winForm.addWindow(frame); winForm.draw(); }}
运行结果
Print WinForm(WINDOW 窗口)print Picture(LOGO 图片)print Button(登录)print Button(注册)print Frame(FRAME1)print Label(用户名)print TextBox(文本框)print Label(密码)print PasswordBox(密码框)print CheckBox(复选框)print TextBox(记住用户名)print LinkLabel(忘记密码)
划线
评论
复制
发布于: 2020 年 06 月 24 日 阅读数: 75
0x12FD16B
关注
还未添加个人签名 2018.01.19 加入
还未添加个人简介
评论