架构师训练营 - 第三周 - 作业 1
发布于: 2020 年 06 月 24 日
1.
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
2.
请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图
//Drawpublic interface Draw { void draw();}//WinFormpublic class WinForm implements Draw { private List<Draw> elements = new ArrayList<>(); private String content; public WinForm(String content) { this.content = content; } public void addElement(Draw element) { elements.add(element); } @Override public void draw() { System.out.println("print WinForm(" + content + ")"); for (Draw element : elements) { element.draw(); } }}//Picturepublic class Picture implements Draw { private String content; public Picture(String content) { this.content = content; } @Override public void draw() { System.out.println("print Picture(" + content + ")"); }}//Buttonpublic class Button implements Draw { private String content; public Button(String content) { this.content = content; } @Override public void draw() { System.out.println("print Button(" + this.content + ")"); }}//Framepublic class Frame implements Draw { private List<Draw> elements = new ArrayList<>(); private String content; public Frame(String content) { this.content = content; } public void addElement(Draw element) { elements.add(element); } @Override public void draw() { System.out.println("print Frame(" + this.content + ")"); for (Draw draw : elements) { draw.draw(); } }}//Lablepublic class Lable implements Draw { private String content; public Lable(String content) { this.content = content; } @Override public void draw() { System.out.println("print Lable(" + this.content + ")"); }}//TextBoxpublic class TextBox implements Draw{ private String content; public TextBox(String content) { this.content = content; } @Override public void draw() { System.out.println("print TextBox("+ this.content +")"); }}//PasswordBoxpublic class PasswordBox implements Draw{ private String content; public PasswordBox(String content) { this.content = content; } @Override public void draw() { System.out.println("print PasswordBox("+ content +")"); }}//CheckBoxpublic class CheckBox implements Draw{ private String content; public CheckBox(String content) { this.content = content; } @Override public void draw() { System.out.println("print CheckBox(" + content + ")"); }}//LinkLablepublic class LinkLable implements Draw { private String content; public LinkLable(String content) { this.content = content; } @Override public void draw() { System.out.println("print LinkLable(" + this.content + ")"); }}//Clientpublic class Client { public static void main(String[] args) { WinForm winForm = new WinForm("WINDOW窗口"); Picture picture = new Picture("LOGO图片"); Button loginBtn = new Button("登录"); Button registBtn = new Button("注册"); Frame frame = new Frame("FRAME1"); winForm.addElement(picture); winForm.addElement(loginBtn); winForm.addElement(registBtn); winForm.addElement(frame); Lable usernameLab = new Lable("用户名"); TextBox textBox = new TextBox("文本框"); Lable passwordLab = new Lable("密码"); PasswordBox passwordBox = new PasswordBox("密码框"); CheckBox checkBox = new CheckBox("复选框"); TextBox remenberBox = new TextBox("记住用户名"); LinkLable linkLable = new LinkLable("忘记密码"); frame.addElement(usernameLab); frame.addElement(textBox); frame.addElement(passwordLab); frame.addElement(passwordBox); frame.addElement(checkBox); frame.addElement(remenberBox); frame.addElement(linkLable); winForm.draw(); }}
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 43
版权声明: 本文为 InfoQ 作者【seng man】的原创文章。
原文链接:【http://xie.infoq.cn/article/847b56d6e0c4093131f4c2c5d】。未经作者许可,禁止转载。
seng man
关注
还未添加个人签名 2018.12.04 加入
还未添加个人简介
评论