架构师训练营 第 3 周作业
发布于: 2020 年 06 月 21 日
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
分别写了饿汉式和懒汉式(双重验证支持线程安全,效率上会比直接在getInstance方法上加synchronized好些)
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
类图设计:
代码目录结构:
代码实现:
public interface UIComponent { void render();}public abstract class AbstractUIComponent implements UIComponent { protected String id; protected String name; public void render() { // 备注:题目要求只需打印一个具体UI组件的类型和名字即可,实际项目中render需要在各个UI组件子类中去实现 System.out.println("print " + this.getClass().getSimpleName() + "(" + this.name + ")"); }}public abstract class SingleUIComponent extends AbstractUIComponent {}public abstract class ContainerUIComponent extends AbstractUIComponent { private List<UIComponent> components = new ArrayList<UIComponent>(); public void add(UIComponent component) { this.components.add(component); } public void remove(UIComponent component) { this.components.remove(component); } @Override public void render() { super.render(); for (UIComponent component : this.components) { component.render(); } }}public class Button extends SingleUIComponent { public Button(String id, String name) { this.id = id; this.name = name; }}public class CheckBox extends SingleUIComponent { public CheckBox(String id, String name) { this.id = id; this.name = name; }}public class Frame extends ContainerUIComponent { public Frame(String id, String name) { this.id = id; this.name = name; }}public class Label extends SingleUIComponent { public Label(String id, String name) { this.id = id; this.name = name; }}public class LinkLabel extends SingleUIComponent { public LinkLabel(String id, String name) { this.id = id; this.name = name; }}public class PasswordBox extends SingleUIComponent { public PasswordBox(String id, String name) { this.id = id; this.name = name; }}public class Picture extends SingleUIComponent { public Picture(String id, String name) { this.id = id; this.name = name; }}public class TextBox extends SingleUIComponent { public TextBox(String id, String name) { this.id = id; this.name = name; }}public class WinForm extends ContainerUIComponent { public WinForm(String id, String name) { this.id = id; this.name = name; }}public class UILauncher { public static void main(String[] args) { ContainerUIComponent winForm = new WinForm("winForm", "WINDOW窗口"); winForm.add(new Picture("picLogo", "LOGO图片")); winForm.add(new Button("btnLogin", "登录")); winForm.add(new Button("btnRegister", "注册")); ContainerUIComponent frame = new Frame("frame1", "FRAME1"); frame.add(new Label("labelUsername", "用户名")); frame.add(new TextBox("textBoxUsername", "文本框")); frame.add(new Label("labelPassword", "密码")); frame.add(new PasswordBox("passwordBox", "密码框")); frame.add(new CheckBox("checkBoxRemember", "复选框")); frame.add(new Label("labelRemember", "记住用户名")); frame.add(new LinkLabel("linkLabelForgot", "忘记密码")); winForm.add(frame); winForm.render(); }}
运行结果:
划线
评论
复制
发布于: 2020 年 06 月 21 日 阅读数: 49
版权声明: 本文为 InfoQ 作者【Lingjun】的原创文章。
原文链接:【http://xie.infoq.cn/article/157637ac0b3001430ec35ccbc】。文章转载请联系作者。
Lingjun
关注
还未添加个人签名 2018.11.22 加入
还未添加个人简介
评论