极客时间架构师训练营 - week3 - 作业 1

用户头像
jjn0703
关注
发布于: 2020 年 06 月 24 日

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

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



习题1解答:



习题2解答:

/**
* 组件接口
*
* @author Jiang Jining
* @date 2020/6/24 7:26
*/
public interface Component {
void print();
}



/**
* 基类
*
* @author Jiang Jining
* @date 2020/6/23 23:47
*/
public abstract class ComponentBase implements Component {
private String name;
private String function;
public ComponentBase(String name, String function) {
this.name = name;
this.function = function;
}
@Override
public void print() {
System.out.println(String.format("print %s(%s)", this.function, this.name));
}
}



import java.util.ArrayList;
import java.util.List;
/**
* 容器, 用于存放各类组件
*
* @author Jiang Jining
* @date 2020/6/24 7:37
*/
public class Container implements Component {
private List<Component> componentList = new ArrayList<>(10);
private String name;
public Container(String name) {
this.name = name;
}
public void addComponent(Component component) {
componentList.add(component);
}
@Override
public void print() {
if (componentList != null) {
componentList.forEach(Component::print);
}
}
}



/**
* Button类
*
* @author Jiang Jining
* @date 2020/6/24 7:46
*/
public class Button extends ComponentBase {
public Button(String name, String function) {
super(name, function);
}
}



/**
* Frame类
*
* @author Jiang Jining
* @date 2020/6/24 7:47
*/
public class Frame extends ComponentBase{
public Frame(String name, String function) {
super(name, function);
}
}



/**
* Label类
*
* @author Jiang Jining
* @date 2020/6/24 7:47
*/
public class Label extends ComponentBase{
public Label(String name, String function) {
super(name, function);
}
}



/**
* TextBox类
*
* @author Jiang Jining
* @date 2020/6/24 7:47
*/
public class TextBox extends ComponentBase {
public TextBox(String name, String function) {
super(name, function);
}
}



/**
* Window类
*
* @author Jiang Jining
* @date 2020/6/24 7:44
*/
public class Window extends ComponentBase {
public Window(String name, String function) {
super(name, function);
}
}



/**
* 输出结果
*
* @author Jiang Jining
* @date 2020/6/24 7:50
*/
public class Print {
public static void main(String[] args) {
Container container = new Container("container...");
container.addComponent(new Window("WINDOW窗口", "WinForm"));
container.addComponent(new Button("LOGO图片", "Picture"));
container.addComponent(new Button("登录", "Button"));
container.addComponent(new Button("注册", "Button"));
container.addComponent(new Frame("FRAME1", "Frame"));
container.addComponent(new Label("用户名", "Label"));
container.addComponent(new TextBox("文本框", "TextBox"));
container.addComponent(new Label("密码", "Label"));
container.addComponent(new TextBox("密码框", "PasswordBox"));
container.addComponent(new TextBox("复选框", "CheckBox"));
container.addComponent(new TextBox("记住用户名", "TextBox"));
container.addComponent(new Label("忘记密码", "LinkLabel"));
container.print();
}
}

程序输出结果:



发布于: 2020 年 06 月 24 日 阅读数: 33
用户头像

jjn0703

关注

Java工程师/终身学习者 2018.03.26 加入

USTC硕士/健身健美爱好者/Java工程师.

评论

发布
暂无评论
极客时间架构师训练营 - week3 - 作业 1