架构师训练营第三周命题作业
发布于: 2020 年 06 月 24 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
2.请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3.
public abstract class Component { private String name; private Integer level; private String type public Component(String name){ this.name = name; this.level = 0; } public void setName(String name) { this.name = name; } public void add(Component component){ throw new UnsupportedOperationException(); } public String getName() { return this.name; } public void setLevel(Integer level) { this.level = level; } public Integer getLevel() { return level; } public abstract void display();}
public class Container extends Component{ public Container(String name, String type) { super(name, type); } @Override public void display(int level) { for (int tmp=0;tmp<level;tmp++){ System.out.print(" "); } System.out.println("print "+getType()+"("+getName()+")"); }}
public class Frame extends Component { List<Component> componentList = new ArrayList<Component>(); public Frame(String name,String type) { super(name,type); } @Override public void add(Component component) { componentList.add(component); } public void display(int level) { for (int tmp=0;tmp<level;tmp++){ System.out.print(" "); } System.out.println("print "+getType()+"("+getName()+")"); componentList.forEach(component -> component.display(level+2)); }}
public class WindowForm extends Component { List<Component> componentsList = new ArrayList(); public WindowForm(String name,String type) { super(name,type); } @Override public void add(Component component) { this.componentsList.add(component); } @Override public void display(int level) { for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println("print "+getType()+"("+getName()+")"); componentsList.forEach(component -> component.display(level+2)); }}
public class App { public static void main(String[] args) { Frame frame = new Frame("FRAME1","Frame"); Container label = new Container("用户名","Label"); frame.add(label); Container textBox = new Container("文本框","TextBox"); frame.add(textBox); Container passLabel = new Container("密码","Label"); frame.add(passLabel); Container passwordBox = new Container("密码框","PasswordBox"); frame.add(passwordBox); Container checkBox = new Container("复选框","CheckBox"); frame.add(checkBox); Container userTextBox= new Container("记住用户名","TextBox"); frame.add(userTextBox); Container linkedLabel = new Container("忘记密码","LinkedLabel"); frame.add(linkedLabel); WindowForm windowForm = new WindowForm("Window 窗口","WindowForm"); windowForm.add(frame); Container picture = new Container("LOGO图片","Picture"); windowForm.add(picture); Container loginButton = new Container("登录","Button"); windowForm.add(loginButton); Container registerButton = new Container("注册","Button"); windowForm.add(registerButton); windowForm.display(4); }}
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 48
版权声明: 本文为 InfoQ 作者【sljoai】的原创文章。
原文链接:【http://xie.infoq.cn/article/54782e99bcab590574c26765a】。文章转载请联系作者。
sljoai
关注
还未添加个人签名 2017.11.09 加入
还未添加个人简介
评论