架构师第三周作业
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
package com.dhc;
public abstract class ObjNode {
protected String nodeName;
public ObjNode(String nodeName){
this.nodeName = nodeName;
}
public abstract void draw();
public String getNodeName(){
return nodeName;
}
}
package com.dhc;
public class Component extends ObjNode {
public Component(String nodeName){
super(nodeName);
}
@Override
public void draw() {
System.out.println("print "+getNodeName());
}
}
package com.dhc;
import java.util.ArrayList;
import java.util.List;
public class Container extends ObjNode {
private List<ObjNode> treeNodes = new ArrayList<>();
public Container(String nodeName){
super(nodeName);
}
public void addSubNode(ObjNode node){
treeNodes.add(node);
}
@Override
public void draw() {
System.out.println("print "+getNodeName());
for(ObjNode node:treeNodes){
node.draw();
}
}
}
package com.dhc;
public class TreeDemo {
public static void main(String[] args) {
Container form = new Container("WinForm(WINDOW窗口)");
Component picture = new Component("Picture(LOGO图片)");
Component button1 = new Component("Button(登陆)");
Component button2 = new Component("Button(注册)");
form.addSubNode(picture);
form.addSubNode(button1);
form.addSubNode(button2);
Container frame = new Container("Frame(FRAME1)");
Component lable1 = new Component("Lable(用户名)");
Component textbox1 = new Component("TextBox(文本框)");
Component lable2 = new Component("Lable(密码)");
Component pwdbox = new Component("PasswordBox(密码框)");
Component chkbox = new Component("CheckBox(复选框)");
Component tbox = new Component("TextBox(记住用户名)");
Component lklable = new Component("LinkLable(忘记密码)");
frame.addSubNode(lable1);
frame.addSubNode(textbox1);
frame.addSubNode(lable2);
frame.addSubNode(pwdbox);
frame.addSubNode(chkbox);
frame.addSubNode(tbox);
frame.addSubNode(lklable);
form.addSubNode(frame);
form.draw();
}
}
版权声明: 本文为 InfoQ 作者【傻傻的帅】的原创文章。
原文链接:【http://xie.infoq.cn/article/a56feea09bb3474e70e23f007】。文章转载请联系作者。
评论