架构师训练营 - 作业 - Week3

发布于: 18 小时前

1, 单例 模式 手写代码

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

打印输出示例参考图 3。

package week3;
import java.util.ArrayList;
public class PrintWinForm {
public static void main(String[] args) {
Container winForm = new Container("WinForm(WINDOW窗口)");
Control picture = new Control("Picture(LOGO图片)");
Control btnLogin = new Control("Button(登录)");
Control btnReg = new Control("Button(注册))");
Container frame = new Container("Frame(FRAME1)");
winForm.add(picture);
winForm.add(btnLogin);
winForm.add(btnReg);
winForm.add(frame);
Control lblUsername = new Control("Label(用户名)");
Control txtUsername = new Control("TextBox(文本框)");
Control lblPassword = new Control("Label(密码)");
Control txtPassword = new Control("PasswordBox(密码框)");
Control chkRememberPassword = new Control("CheckBox(复选框)");
Control lblRememberPassword = new Control("TextBox(记住用户名)");
Control lblForgotPassword = new Control("LinkLabel(忘记密码)");
frame.add(lblUsername);
frame.add(txtUsername);
frame.add(lblPassword);
frame.add(txtPassword);
frame.add(chkRememberPassword);
frame.add(lblRememberPassword);
frame.add(lblForgotPassword);
winForm.print();
}
}
interface Component {
public void print();
}
class Control implements Component {
private String name;
public Control(String name) {
this.name = name;
}
public void print() {
System.out.println("print" + " " + this.name);
}
}
class Container implements Component {
private String name;
private final ArrayList<Component> list = new ArrayList<>();
public Container(String name) {
this.name = name;
}
public void add(Component c) {
list.add(c);
}
public void print() {
System.out.println("print" + " " + this.name);
for (Component component : list) {
component.print();
}
}
}

打印输出:

用户头像

chinsun1

关注

还未添加个人签名 2018.04.25 加入

还未添加个人简介

评论

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