架构师训练营 - 第三课作业 -20200624- 单例及组合模式
发布于: 2020 年 06 月 23 日
作业一:
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
package demo.pattern.composite;public class CompositePatternDemo { public static void main(String[] args) throws IllegalAccessException, InstantiationException { Element logo = new Picture("logo",30, 100,null); Element userLabel = new Element("userLabel",5, 10, "用户", null); Element userTextbox = new Element("userTextbox",5, 30, null); Element passwordLabel = new Element("passwordLabel",5, 10, "密码", null); Element passwordTextbox = new Element("passwordTextbox",5, 30, null); Element RememberUserLabel = new Element("RememberUserLabel",5, 10, "记住用户", null); Element RememberUserCheckbox = new Checkbox("RememberUserCheckbox",5, 30, Boolean.FALSE); Element forgetPLabel = new LinkLabel("forgetPLabel",5, 10, "忘记密码", null, "http://localhost"); Container frame = new Container("frame",80, 100, "#FFFF00"); frame.addElement(userLabel); frame.addElement(userTextbox); frame.addElement(passwordLabel); frame.addElement(passwordTextbox); frame.addElement(RememberUserLabel); frame.addElement(RememberUserCheckbox); frame.addElement(forgetPLabel); Element loginButton = new Button("loginButton",5, 10, "登录", "#FF00FF"); Element signupButton = new Button("signupButton",5, 10, "注册", "#FF00FF"); Container window = new Container("WinForm",180, 180, "#00FF00"); window.addElement(logo); window.addElement(frame); window.addElement(loginButton); window.addElement(signupButton); windown.display(); }}
package demo.pattern.composite;import java.util.ArrayList;import java.util.List;public class Container extends Element { public List<Element> elements = new ArrayList<>(10); public Container(String name, Integer height, Integer width, String backgroundColor) { super(name, height, width, backgroundColor); } public Container(String name, Integer height, Integer width, String backgroundColor, List<Element> elements) { super(name, height, width, backgroundColor); this.elements = elements; } public void addElement(Element element) { this.elements.add(element); } public List<Element> getElements() { return elements; } public void setElements(List<Element> elements) { this.elements = elements; } @Override public void display() { for (Element e : this.elements) { e.display(); } }}
package demo.pattern.composite;public class Element { public String name=null; public Integer height=0; public Integer width=0; public String value=null; public String backgroundColor="#FFFFFF"; public Element(String name, Integer height, Integer width, String value, String backgroundColor) { this.name = name; this.height = height; this.width = width; this.value = value; this.backgroundColor = backgroundColor; } public Element() { } public Element(String name, Integer height, Integer width, String value) { this.name = name; this.height = height; this.width = width; this.value = value; } public Element(String name, Integer height, Integer width) { this.name = name; this.height = height; this.width = width; } public void display() { System.out.println("println "+ this.getName()); }; }
划线
评论
复制
发布于: 2020 年 06 月 23 日阅读数: 75
版权声明: 本文为 InfoQ 作者【👑👑merlan】的原创文章。
原文链接:【http://xie.infoq.cn/article/d93b0dfead43b7317db0c0c5b】。文章转载请联系作者。
👑👑merlan
关注
还未添加个人签名 2018.12.17 加入
还未添加个人简介
评论