架构师训练营 -week3- 作业

发布于: 17 小时前
架构师训练营 -week3- 作业

作业一:请在草稿纸上手写一个单例模式的实现代码。

作业二:请用组合设计模式编写程序,打印输出图1的窗口。

定义IComponent接口:

public interface IComponent {
public void draw();
}

ComponentNode实现IComponent:

public class ComponentNode implements IComponent {
private Vector<IComponent> childList;
private String name;
public ComponentNode(String name) {
// TODO Auto-generated constructor stub
childList = new Vector<>();
this.name = name;
}
public void addChild(IComponent leaf) {
childList.add(leaf);
}
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("print " + this.name);//广度优先输出
for (IComponent iComponent : childList) {
iComponent.draw();
}
}
}

ComponentLeaf实现IComponent:

public class ComponentLeaf implements IComponent {
private String name;
public ComponentLeaf(String name) {
// TODO Auto-generated constructor stub
this.name = name;
}
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println(this.name);
}
}

测试函数:

public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ComponentNode winform = new ComponentNode("winform(window窗体)");
ComponentNode frame = new ComponentNode("frame(FRAME1)");
frame.addChild(new ComponentLeaf("label(用户名)"));
frame.addChild(new ComponentLeaf("textbox(文本框)"));
frame.addChild(new ComponentLeaf("label(密码)"));
frame.addChild(new ComponentLeaf("passwordbox(密码框)"));
frame.addChild(new ComponentLeaf("checkbox(复选框)"));
frame.addChild(new ComponentLeaf("textbox(记住用户)"));
frame.addChild(new ComponentLeaf("linkLabel(忘记密码)"));
winform.addChild(new ComponentLeaf("picture(LOGO图片)"));
winform.addChild(new ComponentLeaf("button(注册)"));
winform.addChild(new ComponentLeaf("button(登录)"));
winform.addChild(frame);
winform.draw();
}
}

输出结果:

发布于: 17 小时前 阅读数: 20
用户头像

晓-Michelle

关注

还未添加个人签名 2020.05.30 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营 -week3- 作业