训练营第三周
发布于: 2020 年 06 月 25 日
1.单例模式
2.请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
//打印器接口public interface Painter { void paint(Component component);}//组件接口public interface Component { String getName(); default void print(Painter painter) { painter.paint(this); }}//窗口组件public class WinForm implements Component { private List<Component> childs = new ArrayList<>(); private String name; public WinForm(String name) { this.name = name; } @Override public String getName() { return this.name; } public void addChild(Component component) { childs.add(component); } @Override public void print(Painter painter) { painter.paint(this); childs.forEach(c -> c.print(painter)); }}//frame 组件public class Frame implements Component { private String name; private List<Component> childs = new ArrayList<>(); public Frame(String name) { this.name = name; } public void addChild(Component component) { childs.add(component); } @Override public void print(Painter painter) { painter.paint(this); childs.forEach(painter::paint); } @Override public String getName() { return this.name; }}public class Button implements Component { private String name; public Button(String name) { this.name = name; } @Override public String getName() { return this.name; }}public class CheckBox implements Component { private String name; public CheckBox(String name) { this.name = name; } @Override public String getName() { return this.name; }}public class Lable implements Component { private String name; public Lable(String name) { this.name = name; } @Override public String getName() { return this.name; }}public class LinkLable implements Component { private String name; public LinkLable(String name) { this.name = name; } @Override public String getName() { return this.name; }}//打印器public class PainterImpl implements Painter { @Override public void paint(Component component) { System.err .println(String.format("print %s (%s)", component.getClass().getSimpleName(), component.getName())); }}public class PasswordBox implements Component { private String name; public PasswordBox(String name) { this.name = name; } @Override public String getName() { return this.name; }}public class Picture implements Component { private String name; public Picture(String name) { this.name = name; } @Override public String getName() { return this.name; }}public class TextBox implements Component { private String name; public TextBox(String name) { this.name = name; } @Override public String getName() { return this.name; }}public class Week3Work2 { public static void main(String[] args) { Painter painter = new PainterImpl(); WinForm winForm = new WinForm("WINDOW窗口"); winForm.addChild(new Picture("LOGO 图片")); winForm.addChild(new Button("登录")); winForm.addChild(new Button("注册LOGO 图片")); Frame frame1 = new Frame("FRAME1"); winForm.addChild(frame1); frame1.addChild(new Lable("用户名")); frame1.addChild(new TextBox("文本框")); frame1.addChild(new Lable("密码")); frame1.addChild(new PasswordBox("密码框")); frame1.addChild(new CheckBox("复选框")); frame1.addChild(new TextBox("记住用户名")); frame1.addChild(new TextBox("忘记密码")); winForm.print(painter); }}
划线
评论
复制
发布于: 2020 年 06 月 25 日阅读数: 44
Just顾
关注
还未添加个人签名 2018.05.06 加入
还未添加个人简介
评论