架构师训练营第三周命题作业
发布于: 2020 年 06 月 24 日
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:FRAME1Lable:用户名TextBox:文本框Lable:密码PasswordBox:密码框CheckBox:复选框TextBox:记住用户名LinkLable:忘记密码
划线
评论
复制
发布于: 2020 年 06 月 24 日 阅读数: 30
hifly
关注
还未添加个人签名 2018.03.08 加入
还未添加个人简介
评论