架构师训练营 - 第三周 - 作业
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
2. 请用组合设计模式编写程序
public class ComponentApplication {
public static void main(String[] args) {
// write your code here
ContainerComponent windowComp=new ContainerComponent("print winform(window 窗口)");
windowComp.addSubComponent(new SimpleComponent("print picture(logo)"));
windowComp.addSubComponent(new SimpleComponent("print button(登陆)"));
windowComp.addSubComponent(new SimpleComponent("print button(注册)"));
ContainerComponent frameComp=new ContainerComponent("print frame");
frameComp.addSubComponent(new SimpleComponent("print label(用户名)"));
frameComp.addSubComponent(new SimpleComponent("print textbox(文本框)"));
frameComp.addSubComponent(new SimpleComponent("print label(密码)"));
frameComp.addSubComponent(new SimpleComponent("print passwordbox(密码框)"));
frameComp.addSubComponent(new SimpleComponent("print checkbox(复选框)"));
frameComp.addSubComponent(new SimpleComponent("print textbox(记住用户名)"));
frameComp.addSubComponent(new SimpleComponent("print linklabel(忘记密码)"));
windowComp.addSubComponent(frameComp);
windowComp.render();
}
}
public interface Component {
void render();
}
public class ContainerComponent implements Component {
private List<Component> childs = new LinkedList<>();
private String text = "";
ContainerComponent(String text) {
this.text = text;
}
public void addSubComponent(Component component) {
this.childs.add(component);
}
@Override
public void render() {
System.out.println(this.text);
childs.forEach(p->p.render());
}
}
public class SimpleComponent implements Component {
private String text = "";
SimpleComponent(String text) {
this.text = text;
}
@Override
public void render() {
System.out.println(this.text);
}
}
评论