架构师训练营 第三周 作业 设计模式

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

微信图片_20201003154740.jpg

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

5.png
public interface Component {
    /**
     * print Component info
     */
    void print();
}
public class BaseComponent implements Component{

    private String name;

    public BaseComponent(String name) {
        this.name = name;
    }

    @Override
    public void print() {
        System.out.println("print " + this.getClass().getSimpleName() + "(" + this.name + ")");
    }
}
public class ComponentContainer extends BaseComponent {

    private final List children = new ArrayList<>();

    public ComponentContainer(String name) {
        super(name);
    }

    public void addChild(Component Component) {
        children.add(Component);
    }

    @Override
    public void print() {
        super.print();
        for (Component child : children) {
            child.print();
        }
    }
}
public class WinForm extends ComponentContainer {

    public WinForm(String name) {
        super(name);
    }
}
public class Button extends BaseComponent{
    public Button(String name) {
        super(name);
    }
}
public class Picture extends BaseComponent{
    public Picture(String name) {
        super(name);
    }
}
public class Frame extends ComponentContainer {

    public Frame(String name) {
        super(name);
    }
}
public class Label extends BaseComponent{
    public Label(String name) {
        super(name);
    }
}
public class TextBox extends BaseComponent{
    public TextBox(String name) {
        super(name);
    }
}
public class PasswordBox extends BaseComponent{
    public PasswordBox(String name) {
        super(name);
    }
}
public class CheckBox extends BaseComponent{
    public CheckBox(String name) {
        super(name);
    }
}
public class LinkLabel extends BaseComponent{
    public LinkLabel(String name) {
        super(name);
    }
}
  public class Test {
    public static void main(String[] args) {
        WinForm form = new WinForm("WINDOW窗口");
        form.addChild(new Picture("LOGO图片"));
        form.addChild(new Button("登陆"));
        form.addChild(new Button("注册"));
        Frame frame = new Frame("FRAME1");
        frame.addChild(new Label("用户名"));
        frame.addChild(new TextBox("文本框"));
        frame.addChild(new Label("密码"));
        frame.addChild(new PasswordBox("密码框"));
        frame.addChild(new CheckBox("复选框"));
        frame.addChild(new TextBox("记住用户名"));
        frame.addChild(new LinkLabel("忘记密码"));
        form.addChild(frame);

        form.print();
    }
}

# 输出如下:
print WinForm(WINDOW窗口)
print Picture(LOGO图片)
print Button(登陆)
print Button(注册)
print Frame(FRAME1)
print Label(用户名)
print TextBox(文本框)
print Label(密码)
print PasswordBox(密码框)
print CheckBox(复选框)
print TextBox(记住用户名)
print LinkLabel(忘记密码)

你可能感兴趣的:(架构师训练营 第三周 作业 设计模式)