架构师训练营第三章作业
发布于: 2020 年 06 月 24 日
手写单例类
利用静态内部类式实现,懒加载,线程安全
使用组合模式打印窗口组件
public interface Component { void draw();}public class Node implements Component{ private List<Component> children; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Node(String name) { this.name = name; this.children = new ArrayList<>(); } public void addChild(Component component){ this.children.add(component); } @Override public void draw() { System.out.println("print "+ this.name); this.children.forEach(Component::draw); }}public class Leaf implements Component{ private String name; public Leaf(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void draw() { System.out.println("print " + this.name); }}public class Test { public static void main(String[] args) { Node winForm = new Node("WinForm(WINDOW窗口)"); winForm.addChild(new Leaf("Picture(Logo图片)")); winForm.addChild(new Leaf("Button(登录)")); winForm.addChild(new Leaf("Button(注册)")); Node frame = new Node("Frame(FRAME1)"); frame.addChild(new Leaf("Label(用户名)")); frame.addChild(new Leaf("TextBox(文本框)")); frame.addChild(new Leaf("Label(密码)")); frame.addChild(new Leaf("PasswordBox(密码框)")); frame.addChild(new Leaf("Checkbox(复选框)")); frame.addChild(new Leaf("TextBox(记住用户名)")); frame.addChild(new Leaf("LinkLabel(忘记密码)")); winForm.addChild(frame); winForm.draw(); }}
..................................................................................................................................................................................
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 42
JUN
关注
还未添加个人签名 2017.12.06 加入
还未添加个人简介
评论