架构师训练营 - 作业 - 第三周
发布于: 2020 年 10 月 03 日
1. 在草稿纸上手写一个单例模式的实现代码,拍照提交
使用DCL实现单例
2、用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3
组合模式中,Componet、Composite、Leaf各角色如下:
Component角色
public abstract class AbstractFrameComponet { private String type; private String caption; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getCaption() { return caption; } public void setCaption(String caption) { this.caption = caption; } public AbstractFrameComponet add(AbstractFrameComponet componet) { throw new UnsupportedOperationException("不支持该操作"); } public abstract void printComponetInfo();}
Leaf角色
public class ConcreteFrameComponent extends AbstractFrameComponet { public ConcreteFrameComponent(String type, String caption) { setType(type); setCaption(caption); } @Override public void printComponetInfo() { System.out.println("print " + getType() + "(" + getCaption() + ")"); } } class Label extends ConcreteFrameComponent { public Label(String caption) { super("Label", caption); }}class TextBox extends ConcreteFrameComponent { public TextBox(String caption) { super("TextBox", caption); }}class PasswordBox extends ConcreteFrameComponent { public PasswordBox(String caption) { super("PasswordBox", caption); }}class CheckBox extends ConcreteFrameComponent { public CheckBox(String caption) { super("CheckBox", caption); }}class LinkLabel extends ConcreteFrameComponent { public LinkLabel(String caption) { super("LinkLabel", caption); }}class Picture extends ConcreteFrameComponent { public Picture(String caption) { super("Picture", caption); }}class Button extends ConcreteFrameComponent { public Button(String caption) { super("Button", caption); }}
Composite角色
public class ConcreteFrameContainer extends AbstractFrameComponet { protected ArrayList<AbstractFrameComponet> childComponetList = new ArrayList<>(); public ConcreteFrameContainer(String type, String caption) { setType(type); setCaption(caption); } @Override public void printComponetInfo() { System.out.println("print " + getType() + "(" + getCaption() + ")"); childComponetList.stream().forEach(AbstractFrameComponet::printComponetInfo); } @Override public AbstractFrameComponet add(AbstractFrameComponet componet) { childComponetList.add(componet); return this; }}class Frame extends ConcreteFrameContainer { public Frame(String caption) { super("Frame", caption); }}class WinForm extends ConcreteFrameContainer { public WinForm(String caption) { super("WinForm", caption); }}
Client
public class Client { public static void main(String[] args) { Frame frame = new Frame("FRAME1"); frame.add(new Label("用户名")) .add(new TextBox("文本框")) .add(new Label("密码")) .add(new PasswordBox("密码框")) .add(new CheckBox("复选框")) .add(new TextBox("记住用户名")) .add(new LinkLabel("忘记密码")); WinForm winForm = new WinForm("WINDOWS窗口"); winForm.add(new Picture("LOGO图片")) .add(new Button("登录")) .add(new Button("注册")) .add(frame); winForm.printComponetInfo(); }}
划线
评论
复制
发布于: 2020 年 10 月 03 日阅读数: 35
Max2@12
关注
还未添加个人签名 2018.12.13 加入
还未添加个人简介
评论