写点什么

「架构师训练营」第 3 周作业 - 组合模式

用户头像
森林
关注
发布于: 2020 年 06 月 25 日

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

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

package com.company;

/**
* @Author: forest
* @CreateTime: 2020-06-24 23:48
* @Description: 窗口
*/
public interface Form {

void addPart(Form form);
void display();

}




package com.company;

import java.util.ArrayList;
import java.util.List;

/**
* @Author: forest
* @CreateTime: 2020-06-24 23:49
* @Description:
*/
public class Button implements Form {

private List<Form> list = new ArrayList<>();

private String name;

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

@Override
public void addPart(Form form) {
list.add(form);
}

@Override
public void display() {
for (Form f : list) {
f.display();
}
}
}




package com.company;

/**
* @Author: forest
* @CreateTime: 2020-06-25 00:03
* @Description: 点击按钮
*/
public class ClickButtion implements Form {

private String name;

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

@Override
public void addPart(Form form) {

}

@Override
public void display() {
System.out.println(name);
}
}




package com.company;

public class Main {

public static void main(String[] args) {

Button root = new Button("WinForm");

ClickButtion b1 = new ClickButtion("Picture");
ClickButtion b2 = new ClickButtion("Button");
ClickButtion b3 = new ClickButtion("Button");

Button broot = new Button("Frame");

root.addPart(b1);
root.addPart(b2);
root.addPart(b3);
root.addPart(broot);

ClickButtion button = new ClickButtion("1");
ClickButtion button1 = new ClickButtion("2");
ClickButtion button2 = new ClickButtion("3");

Button broot3 = new Button("Frame");

broot.addPart(button);
broot.addPart(button1);
broot.addPart(button2);
broot.addPart(broot3);

ClickButtion button4 = new ClickButtion("4");
ClickButtion button5 = new ClickButtion("5");
ClickButtion button6 = new ClickButtion("6");
broot3.addPart(button4);
broot3.addPart(button5);
broot3.addPart(button6);

root.display();


}
}




用户头像

森林

关注

还未添加个人签名 2018.05.07 加入

还未添加个人简介

评论

发布
暂无评论
「架构师训练营」第3 周作业 - 组合模式