写点什么

架构师训练营 - 作业 - 第三讲

用户头像
吕浩
关注
发布于: 2020 年 06 月 23 日

作业一:

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

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



1,


2,首先定义接口

public interface WindowElement {    void print();    void addLeaf(WindowElement element);    void removeLeaf(WindowElement element);    void showLeaf();}
复制代码

然后定义通用组件的实现类

import java.util.ArrayList;import java.util.List;
public abstract class ComponentsElement implements WindowElement { private List<WindowElement> leafList = new ArrayList<>(); private String name ;
public ComponentsElement(String name){ this.name = name; }
@Override public void print() { System.out.println("print " + this.getClass().getSimpleName() +"(" + this.name +")"); for (WindowElement leaf:leafList) { leaf.print(); } }
@Override public void addLeaf(WindowElement element) { leafList.add(element); }
@Override public void removeLeaf(WindowElement element) { leafList.remove(element); }
@Override public void showLeaf() { for (WindowElement leaf:leafList) { leaf.showLeaf(); } }}
复制代码

下面挨个定义所有组件,按字母顺序写在下面了。

public class Button extends ComponentsElement {    public Button(String name) {        super(name);    }}public class CheckBox extends ComponentsElement {    public CheckBox(String name) {        super(name);    }}public class Frame extends ComponentsElement {    public Frame(String name) {        super(name);    }}public class Lable extends ComponentsElement {    public Lable(String name) {        super(name);    }}public class LinkLable extends ComponentsElement {    public LinkLable(String name) {        super(name);    }}public class PasswordBox extends ComponentsElement {    public PasswordBox(String name) {        super(name);    }}public class Picture extends ComponentsElement {    public Picture(String name) {        super(name);    }}public class TextBox extends ComponentsElement {    public TextBox(String name) {        super(name);    }}public class WinForm extends ComponentsElement {    public WinForm(String name) {        super(name);    }}
复制代码

最后是测试类

public class test {    public static void main(String[] args) {        ComponentsElement window = new WinForm("WINDOW窗口");        window.addLeaf(new Picture("LOGO图片"));        window.addLeaf(new Button("登录"));        window.addLeaf(new Button("注册"));        ComponentsElement frame1 = new Frame("FRAME1");        frame1.addLeaf(new Lable("用户名"));        frame1.addLeaf(new TextBox("文本框"));        frame1.addLeaf(new Lable("密码"));        frame1.addLeaf(new PasswordBox("密码框"));        frame1.addLeaf(new CheckBox("复选框"));        frame1.addLeaf(new TextBox("记住用户名"));        frame1.addLeaf(new LinkLable("忘记密码"));        window.addLeaf(frame1);        window.print();    }}
复制代码

输出结果:

print WinForm(WINDOW窗口)print Picture(LOGO图片)print Button(登录)print Button(注册)print Frame(FRAME1)print Lable(用户名)print TextBox(文本框)print Lable(密码)print PasswordBox(密码框)print CheckBox(复选框)print TextBox(记住用户名)print LinkLable(忘记密码)
Process finished with exit code 0
复制代码


用户头像

吕浩

关注

还未添加个人签名 2018.04.27 加入

还未添加个人简介

评论

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