架构师训练营第三周作业
作业一:
手写单例模式
作业二:
组合模式的代码实现
public abstract class Window{
protected String id;
public Window(String id) {
this.id = id;
}
public String getId() {
return id;
}
public abstract boolean windowPrint();
}
public class Frame extends Window{
public Frame(String id) {
super(id);
}
@Override
public boolean windowPrint() {
System.out.println(id);
return true;
}
}
public class WindowForm extends Window{
private List<Window> subNodes = new ArrayList<>();
public WindowForm(String id) {
super(id);
}
@Override
public boolean windowPrint() {
for(Window window : subNodes) {
System.out.println(window.id);
}
return true;
}
public void addSubNode(Window window) {
subNodes.add(window);
}
}
public class Demo {
public static void main(String[] args) {
WindowForm windowTree = new WindowForm("window form");
WindowForm nodeFrame = new WindowForm("frame");
Frame picture = new Frame("picture");
Frame button1 = new Frame("button");
Frame button2 = new Frame("button");
windowTree.addSubNode(picture);
windowTree.addSubNode(button1);
windowTree.addSubNode(button2);
Frame label1 = new Frame("label");
Frame label2 = new Frame("label");
Frame label3 = new Frame("label");
Frame textBox = new Frame("textbox");
Frame passWordBox = new Frame("passwordbox");
Frame checkBox = new Frame("checkbox");
Frame linkLable = new Frame("linklable");
nodeFrame.addSubNode(label1);
nodeFrame.addSubNode(textBox);
nodeFrame.addSubNode(label2);
nodeFrame.addSubNode(passWordBox);
nodeFrame.addSubNode(checkBox);
nodeFrame.addSubNode(label3);
nodeFrame.addSubNode(linkLable);
windowTree.addSubNode(nodeFrame);
windowTree.windowPrint();
nodeFrame.windowPrint();
}
}
版权声明: 本文为 InfoQ 作者【James-Pang】的原创文章。
原文链接:【http://xie.infoq.cn/article/0ab14ff916599071457ec413c】。未经作者许可,禁止转载。
评论