架构师训练营 - 第三周作业
发布于: 2020 年 10 月 01 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
class Component { private Component(){} public Component(String title,String detail){ this.title=title; this.detail=detail; } private String title; private String detail; private List<Component> componentList=new ArrayList<>(); public void print() { System.out.printf("print %s(%s)\n",title,detail); for(Component obj:componentList){ obj.print(); } } public void addComponent(Component component) { this.componentList.add(component); }}public class Demo { public static void main(String[] args) { Component winForm = new Component("WinForm","WINDOW窗口"); Component picture = new Component("Picture","LOGO图片"); Component button1 = new Component("Button","登录"); Component button2 = new Component("Button","注册"); Component frame = new Component("Frame","FRAME1"); Component lable1 = new Component("Lable","用户名"); Component textBox1 = new Component("TextBox","文本框"); Component lable2 = new Component("Lable","密码"); Component passwordBox = new Component("PasswordBox","密码框"); Component checkBox = new Component("CheckBox","复选框"); Component textBox2 = new Component("TextBox","记住用户名"); Component linkLable = new Component("LinkLable","忘记密码"); winForm.addComponent(picture); winForm.addComponent(button1); winForm.addComponent(button2); winForm.addComponent(frame); frame.addComponent(lable1); frame.addComponent(textBox1); frame.addComponent(lable2); frame.addComponent(passwordBox); frame.addComponent(checkBox); frame.addComponent(textBox2); frame.addComponent(linkLable); winForm.print(); }}
输出结果:
划线
评论
复制
发布于: 2020 年 10 月 01 日阅读数: 33
一个节点
关注
还未添加个人签名 2020.07.27 加入
还未添加个人简介
评论