架构师训练营 - 作业 3

用户头像
进击的炮灰
关注
发布于: 2020 年 06 月 25 日
架构师训练营-作业3

1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。







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



/*
** Interface
*/
public interface IComponent {
void printTree();
}



import java.util.ArrayList;
import java.util.List;
/*
**Container component, contain plain function component in it
*/
public class ContainerComponent implements IComponent{
private List<IComponent> list = new ArrayList<IComponent>();
private String name;
public ContainerComponent(String name){
this.name = name;
}
public ContainerComponent addSubComponent(IComponent component){
list.add(component);
return this;
}
@Override
public void printTree() {
System.out.println("print "+name);
list.forEach(IComponent::printTree);
}
}
/*
** Plain function component
*/
public class FunctionComponent implements IComponent{
private String name;
public FunctionComponent(String name){
this.name = name;
}
@Override
public void printTree() {
System.out.println("print "+name);
}
}
/*
** Test print the whole component name in tree structure
*/
public class Test {
public static void main(String[] args) {
IComponent userNameLabel = new FunctionComponent("Lable(用户名)");
IComponent userNameTextBox = new FunctionComponent("TextBox(文本框)");
IComponent pwdLabel = new FunctionComponent("Lable(密码)");
IComponent pwdTextBox = new FunctionComponent("PasswordBox(密码框)");
IComponent remeberUserCheckBox = new FunctionComponent("CheckBox(复选框)");
IComponent remeberUserTextBox = new FunctionComponent("TextBox(记住用户名)");
IComponent forgetPwdLinkLabel = new FunctionComponent("LinkLable(忘记密码)");
IComponent frame = new ContainerComponent("Frame(FRAME1)")
.addSubComponent(userNameLabel).addSubComponent(userNameTextBox)
.addSubComponent(pwdLabel).addSubComponent(pwdTextBox)
.addSubComponent(remeberUserCheckBox).addSubComponent(remeberUserTextBox)
.addSubComponent(forgetPwdLinkLabel);
IComponent logoPicture = new FunctionComponent("Picture(LOGO图片)");
IComponent loginButton = new FunctionComponent("Button(登录)");
IComponent registerButton = new FunctionComponent("Button(注册)");
IComponent winForm = new ContainerComponent("WinForm(WINDOW窗口)")
.addSubComponent(logoPicture)
.addSubComponent(loginButton)
.addSubComponent(registerButton)
.addSubComponent(frame);
winForm.printTree();
}
}



用户头像

进击的炮灰

关注

还未添加个人签名 2020.05.13 加入

还未添加个人简介

评论

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