架构师训练营第三周命题作业

发布于: 14 小时前
架构师训练营第三周命题作业

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

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

定义抽象类WindowsNode,包含type和name两个属性和printNode抽象方法,用来表示抽象的窗口结点。

package composite;
public abstract class WindowsNode {
private String type;
private String name;
public WindowsNode(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
public abstract void printNode();
}

WindowsWidget类继承了WindowsNode类,重写了printNode方法,用来表示具体的最小窗口对象。

package composite;
public class WindowsWidget extends WindowsNode {
public WindowsWidget(String type, String name) {
super(type, name);
}
@Override
public void printNode() {
System.out.println(this.getType() + ":" + this.getName());
}
}

WindowsWidget类继承了WindowsNode类,重写了printNode方法,新增了WindowsNodes属性和addSubNode方法,用来表示一组窗口对象。mian函数用来对整个组合模式的程序进行测试。

package composite;
import java.util.ArrayList;
import java.util.List;
public class WindowsComponents extends WindowsNode {
private List<WindowsNode> WindowsNodes = new ArrayList<>();
public WindowsComponents(String type, String name) {
super(type, name);
}
public void addSubNode(WindowsNode windowsNode) {
WindowsNodes.add(windowsNode);
}
@Override
public void printNode() {
System.out.println(this.getType() + ":" + this.getName());
for (WindowsNode windowsNode : WindowsNodes) {
windowsNode.printNode();
}
}
public static void main(String[] args) {
WindowsComponents winForm = new WindowsComponents("WinForm", "WINDOW窗口");
WindowsWidget picture = new WindowsWidget("Picture", "LOGO图片");
WindowsWidget loginButton = new WindowsWidget("Button", "登录");
WindowsWidget registerButton = new WindowsWidget("Button", "注册");
WindowsComponents frame = new WindowsComponents("Frame", "FRAME1");
winForm.addSubNode(picture);
winForm.addSubNode(loginButton);
winForm.addSubNode(registerButton);
winForm.addSubNode(frame);
WindowsWidget userLable = new WindowsWidget("Lable", "用户名");
WindowsWidget userTextBox = new WindowsWidget("TextBox", "文本框");
WindowsWidget pwdLable = new WindowsWidget("Lable", "密码");
WindowsWidget pwdBox = new WindowsWidget("PasswordBox", "密码框");
WindowsWidget checkBox = new WindowsWidget("CheckBox", "复选框");
WindowsWidget rememberTextBox = new WindowsWidget("TextBox", "记住用户名");
WindowsWidget forgetPwdLable = new WindowsWidget("LinkLable", "忘记密码");
frame.addSubNode(userLable);
frame.addSubNode(userTextBox);
frame.addSubNode(pwdLable);
frame.addSubNode(pwdBox);
frame.addSubNode(checkBox);
frame.addSubNode(rememberTextBox);
frame.addSubNode(forgetPwdLable);
winForm.printNode();
}
}

运行main函数,打印结果如下:

WinForm:WINDOW窗口
Picture:LOGO图片
Button:登录
Button:注册
Frame:FRAME1
Lable:用户名
TextBox:文本框
Lable:密码
PasswordBox:密码框
CheckBox:复选框
TextBox:记住用户名
LinkLable:忘记密码

用户头像

hifly

关注

还未添加个人签名 2018.03.08 加入

还未添加个人简介

评论

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