写点什么

架构师训练营第三周作业

用户头像
关注
发布于: 2020 年 10 月 05 日

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

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





单例模式

组合模式

package com.cava.three.component;
public interface Component {
void print();
}



package com.cava.three.component;
public abstract class AbstractComponent implements Component {
private String name;
public AbstractComponent(String name) {
this.name = name;
}
@Override
public void print() {
System.out.println("print " + this.getClass().getSimpleName() + "(" + name + ")");
}
}



package com.cava.three.component;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractComponentContainer extends AbstractComponent {
private List<Component> components = new ArrayList<Component>();
public AbstractComponentContainer(String name) {
super(name);
}
public void addComponent(Component component) {
this.components.add(component);
}
@Override
public void print() {
super.print();
for (Component component : components) {
component.print();
}
}
}



package com.cava.three.component;
public class Button extends AbstractComponent {
public Button(String name) {
super(name);
}
}



package com.cava.three.component;
public class CheckBox extends AbstractComponent {
public CheckBox(String name) {
super(name);
}
}



package com.cava.three.component;
public class Label extends AbstractComponent {
public Label(String name) {
super(name);
}
}



package com.cava.three.component;
public class LinkLabel extends Label {
public LinkLabel(String name) {
super(name);
}
}



package com.cava.three.component;
public class PasswordBox extends AbstractComponent {
public PasswordBox(String name) {
super(name);
}
}



package com.cava.three.component;
public class Picture extends AbstractComponent {
public Picture(String name) {
super(name);
}
}



package com.cava.three.component;
public class TextBox extends AbstractComponent {
public TextBox(String name) {
super(name);
}
}



package com.cava.three.component;
public class Frame extends AbstractComponentContainer {
public Frame(String name) {
super(name);
}
}



package com.cava.three.component;
public class WinForm extends AbstractComponentContainer {
public WinForm(String name) {
super(name);
}
}



ackage com.cava.three;
import com.cava.three.component.*;
public class CompositeWindow {
public static void main(String[] args) {
WinForm winForm = buildWinForm();
winForm.print();
}
private static WinForm buildWinForm() {
WinForm winForm = new WinForm("WINDOW窗口");
winForm.addComponent(new Picture("LOGO图片"));
winForm.addComponent(new Button("登录"));
winForm.addComponent(new Button("注册"));
winForm.addComponent(new Frame("FRAME1"));
winForm.addComponent(new Label("用户名"));
winForm.addComponent(new TextBox("文本框"));
winForm.addComponent(new Label("密码"));
winForm.addComponent(new PasswordBox("密码框"));
winForm.addComponent(new CheckBox("复选框"));
winForm.addComponent(new TextBox("记住用户名"));
winForm.addComponent(new LinkLabel("忘记密码"));
return winForm;
}
}



发布于: 2020 年 10 月 05 日阅读数: 29
用户头像

关注

还未添加个人签名 2018.03.09 加入

还未添加个人简介

评论

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