「架构师训练营」第 3 周作业

发布于: 1 小时前

作业:

1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。

一、手写实现单例模式

二、使用组合模式编写程序

抽象元素类

public abstract class AbstractElement {
protected String name;
protected List<AbstractElement> subAbstractElements;
public AbstractElement(String name) {
this.name = name;
}
public final void print() {
this.doPrint();
if (CollectionUtils.isNotEmpty(subAbstractElements)) {
for (AbstractElement subAbstractElement : subAbstractElements) {
subAbstractElement.print();
}
}
}
protected abstract void doPrint();
}

窗口类

public class WinFrom extends AbstractElement {
public WinFrom(String name) {
super(name);
this.subAbstractElements = new LinkedList<>();
}
public void addSubElement(AbstractElement abstractElement) {
this.subAbstractElements.add(abstractElement);
}
@Override
protected void doPrint() {
System.out.println("print WinFrom(" + this.name + ")");
}
}

图片类

public class Picture extends AbstractElement {
public Picture(String name) {
super(name);
}
@Override
protected void doPrint() {
System.out.println("print Picture(" + this.name + ")");
}
}

按钮类

public class Button extends AbstractElement {
public Button(String name) {
super(name);
}
@Override
protected void doPrint() {
System.out.println("print Button(" + this.name + ")");
}
}

框类

public class Frame extends AbstractElement {
public Frame(String name) {
super(name);
this.subAbstractElements = new LinkedList<>();
}
public void addSubElement(AbstractElement abstractElement) {
this.subAbstractElements.add(abstractElement);
}
@Override
protected void doPrint() {
System.out.println("print Frame(" + this.name + ")");
}
}

标签类

public class Label extends AbstractElement {
public Label(String name) {
super(name);
}
@Override
protected void doPrint() {
System.out.println("print Label(" + this.name + ")");
}
}

文本框

public class TextBox extends AbstractElement {
public TextBox(String name) {
super(name);
}
@Override
protected void doPrint() {
System.out.println("print TextBox(" + this.name + ")");
}
}

密码框

public class PasswordBox extends AbstractElement {
public PasswordBox(String name) {
super(name);
}
@Override
protected void doPrint() {
System.out.println("print PasswordBox(" + this.name + ")");
}
}

复选框

public class CheckBox extends AbstractElement {
public CheckBox(String name) {
super(name);
}
@Override
protected void doPrint() {
System.out.println("print CheckBox(" + this.name + ")");
}
}

带链接文本框

public class LinkLabel extends AbstractElement {
public LinkLabel(String name) {
super(name);
}
@Override
protected void doPrint() {
System.out.println("print LinkLabel(" + this.name + ")");
}
}

测试类

public class Demo {
public static void main(String[] args) {
WinFrom window = new WinFrom("WINDOW窗口");
Picture logo = new Picture("LOGO图片");
Frame frame = new Frame("FRAME1");
Button loginBtn = new Button("登录");
Button registerBtn = new Button("注册");
window.addSubElement(logo);
window.addSubElement(loginBtn);
window.addSubElement(registerBtn);
window.addSubElement(frame);
Label userNameLabel = new Label("用户名");
Label passwordLabel = new Label("密码");
TextBox userNameTextBox = new TextBox("文本框");
PasswordBox password = new PasswordBox("密码框");
CheckBox checkBox = new CheckBox("复选框");
TextBox remember = new TextBox("记住用户名");
LinkLabel linkLabel = new LinkLabel("忘记密码");
frame.addSubElement(userNameLabel);
frame.addSubElement(userNameTextBox);
frame.addSubElement(passwordLabel);
frame.addSubElement(password);
frame.addSubElement(checkBox);
frame.addSubElement(remember);
frame.addSubElement(linkLabel);
window.print();
}
}

用户头像

邓江川。

关注

还未添加个人签名 2018.06.15 加入

还未添加个人简介

评论

发布
暂无评论
「架构师训练营」第 3 周作业