架构师训练营第 03周作业

  1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
    单例模式
IMG_2115.JPG
  1. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。


    image.png

最后结果


image.png

代码如下
IComponent.java

interface IComponent{
    
    public void add(IComponent component);   
    public void remove(IComponent component);  
    public void display();   
}

Window.java

import java.util.ArrayList;

class Window implements IComponent{


    private String name;

    public Window(String name){
        this.name = name;
    }
    private ArrayList<IComponent> componentArrayList = new ArrayList<IComponent>();


    @Override
    public void add(IComponent component){
        this.componentArrayList.add(component);
    }

    @Override
    public void remove(IComponent component){
        this.componentArrayList.remove(component);
    };  

    @Override
    public void display(){
        System.out.println(name);
        //下级遍历
        for (IComponent component : componentArrayList) {
            // System.out.println("xxxx"+component.);
            component.display();
        }
    };  

}

Client.java


class Client {

    public Client(){

    }
    public static void main(String[] args) {

        Window winForm = new Window("WinForm(WINDOW窗口)");
        Window picture = new Window("Picture(LOGO图片)");
        Window button1 = new Window("Button(登录)");
        Window button2 = new Window("Button(注册)");
        Window frame = new Window("Frame(FRAME1)");
        Window lable1 = new Window("Lable(用户名)");
        Window textbox1 = new Window("TextBox(文本框)");
        Window lable2 = new Window("Lable(密码)");
        Window passwordBox = new Window("PasswordBox(密码框)");
        Window checkBox = new Window("CheckBox(复选框)");
        Window textbox2 = new Window("TextBox(记住密码)");
        Window linkLable = new Window("LinkLable(忘记密码)");

        winForm.add(picture);
        winForm.add(button1);
        winForm.add(button2);
        winForm.add(frame);
        frame.add(lable1);
        frame.add(textbox1);
        frame.add(lable2);
        frame.add(passwordBox);
        frame.add(checkBox);
        frame.add(textbox2);
        frame.add(linkLable);
        winForm.display();
    }
    
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。