架构师训练营 1 期 - 第三周作业(vaik)
发布于: 2020 年 10 月 04 日
作业一,手写单例模式
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
作业
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
窗口组件接口类
package com.vaik.compositepatterndemo;import java.util.ArrayList;public interface IWindowComponent { void add(IWindowComponent component); void remove(IWindowComponent component); IWindowComponent getChild(int i); String getName(); void print();}
窗口组件类
package com.vaik.compositepatterndemo;import java.util.ArrayList;public class WindowComponent implements IWindowComponent{ String name; String typeName; public WindowComponent(String typeName,String name){ this.name = name; this.typeName = typeName; } ArrayList<IWindowComponent> components = new ArrayList<>(); @Override public void add(IWindowComponent component) { components.add(component); } @Override public void remove(IWindowComponent component) { components.remove(component); } @Override public IWindowComponent getChild(int i) { return components.get(i); } @Override public String getName() { return this.name; } @Override public void print() { System.out.println("print "+this.typeName+"("+this.name+")"); for (IWindowComponent component:components) { component.print(); } }}
测试输出
package com.vaik.compositepatterndemo;public class CompositePatternDemo { public static void main(String[] args){ WindowComponent winForm = new WindowComponent("WinForm","WINDOWS窗口"); WindowComponent picture = new WindowComponent("Picture","LOGO图片"); WindowComponent button1 = new WindowComponent("Button","登录"); WindowComponent button2 = new WindowComponent("Button","注册"); WindowComponent frame1 = new WindowComponent("Frame","FRAME1"); WindowComponent label1 = new WindowComponent("Lable","用户名"); WindowComponent textBox1 = new WindowComponent("TextBox","文本框"); WindowComponent label2 = new WindowComponent("Lable","密码"); WindowComponent passwordBox = new WindowComponent("PasswordBox","文本框"); WindowComponent checkBox = new WindowComponent("CheckBox","复选框"); WindowComponent textBox2 = new WindowComponent("TextBox","记住用户名"); WindowComponent linkLable = new WindowComponent("LinkLable","忘记密码"); frame1.add(label1); frame1.add(textBox1); frame1.add(label2); frame1.add(passwordBox); frame1.add(checkBox); frame1.add(textBox1); frame1.add(linkLable); winForm.add(picture); winForm.add(button1); winForm.add(button2); winForm.add(frame1); winForm.print(); }}
划线
评论
复制
发布于: 2020 年 10 月 04 日 阅读数: 25
行之
关注
还未添加个人签名 2018.09.18 加入
还未添加个人简介
评论