架构师训练营——第三周作业

发布于: 2020 年 06 月 23 日

1、在草稿纸上手写一个单例模式实现代码

2、请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。

public class Test {
public static void main(String[] args) {
Leaf pictureLogo = new Leaf("Picture(LOGO图片)");
Leaf Button = new Leaf("Button(登录)");
Leaf registerButton = new Leaf("Button(注册)");
Leaf labelName = new Leaf("Label(用户名)");
Leaf textBoxName = new Leaf("TextBox(文本框)");
Leaf labelPass = new Leaf("Label(密码)");
Leaf passwordBox = new Leaf("PasswordBox(密码框)");
Leaf checkBox = new Leaf("CheckBox(复选框)");
Leaf textBox = new Leaf("TextBox(记住用户名)");
Leaf linkLable = new Leaf("LinkLable(忘记密码)");
List<Component> list1 = new ArrayList<Component>();
list1.add(labelName);
list1.add(textBoxName);
list1.add(labelPass);
list1.add(passwordBox);
list1.add(checkBox);
list1.add(textBox);
list1.add(linkLable);
Container Frame = new Container("FRAME1",list1);
List<Component> list2 = new ArrayList<Component>();
list2.add(pictureLogo);
list2.add(Button);
list2.add(registerButton);
list2.add(Frame);
Container winForm = new Container("WINDOW窗口",list2);
winForm.displayPart();
}
}

public interface Component {
public void displayPart();
}

public class Container implements Component{
private String name;
private List<Component> partList;
public Container(String name,List<Component> partList){
this.partList = new ArrayList<Component>();
this.name = name;
this.partList.addAll(partList);
}
public void addPart(Component component) {
this.partList.add(component);
}
@Override
public void displayPart() {
log.info("print {}",this.name);
this.partList.forEach(part -> part.displayPart());
}
}

public class Leaf implements Component{
private String name;
public Leaf(String name) {
this.name = name;
}
@Override
public void displayPart() {
log.info("print {}",this.name);
}
}

用户头像

jiangnanage

关注

还未添加个人签名 2019.04.11 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营——第三周作业