写点什么

「架构师训练营第 1 期」第三周作业

用户头像
张国荣
关注
发布于: 2020 年 10 月 07 日

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



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



mport java.util.ArrayList;
import java.util.List;

public class Component {
String name; //组件名称
String type; //组件类型
List<Component> subComponent; //子组件

public Component(String name, String type) {
this.name = name;
this.type = type;
}

public void print() {
//先输出父组件,在遍历子组件
System.out.println("print " + type + "(" + name + ")");
if (subComponent != null) {
for (Component component : subComponent) {
component.print();
}
}
}

//添加子组件
public Component addSubComponent(Component newComponent) {
if (subComponent == null) {
subComponent = new ArrayList<>();
}
subComponent.add(newComponent);
return this;
}

public static void main(String args[]) {

Component window = new Component("WINDOW窗口", "WinForm");
Component picture = new Component("LOGO图片", "Picture");
Component buttonLogin = new Component("登陆", "Button");
Component buttonRegister = new Component("注册", "Button");
Component frame = new Component("Frame1", "Frame");
window.addSubComponent(picture)
.addSubComponent(buttonLogin)
.addSubComponent(buttonRegister)
.addSubComponent(frame);
Component userNameLabel = new Component("用户名", "Label");
Component textBox = new Component("文本框", "TextBox");
Component passwordLabel = new Component("密码", "Label");
Component passwordBox = new Component("密码框", "PasswordBox");
Component checkbox = new Component("复选框", "CheckBox");
Component textBoxRemember = new Component("记住用户名", "TextBox");
Component forgetPassword = new Component("忘记密码", "LinkLable");
frame.addSubComponent(userNameLabel)
.addSubComponent(textBox)
.addSubComponent(passwordLabel)
.addSubComponent(passwordBox)
.addSubComponent(checkbox)
.addSubComponent(textBoxRemember)
.addSubComponent(forgetPassword);
window.print();
}
}



运行结果:



用户头像

张国荣

关注

还未添加个人签名 2018.06.26 加入

还未添加个人简介

评论

发布
暂无评论
「架构师训练营第 1 期」第三周作业