写点什么

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

用户头像
kk
关注
发布于: 2020 年 06 月 24 日
架构师训练营 - 第三周作业

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


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


最终实现的代码

public class Application {    public static void main(String[] args) {        CompoundWidget winForm = new WinForm("WINDOW窗口");        winForm.add(                new Picture("LOGO图片"),                new Button("登录"),                new Button("注册"),                new Frame("FRAME1",                    new Label("用户名"),                    new TextBox("文本框"),                    new Label("密码"),                    new PasswordBox("密码框"),                    new CheckBox("复选框"),                    new TextBox("记住用户名"),                    new LinkLabel("忘记密码")                )        );        winForm.render();    }    /**     * ==========程序运行结果=========     *      * 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(忘记密码)     */}
复制代码

设计抽象:Widget -> BaseWidget -> CompoundWidget

interface Widget {    String getName();    void render();}
abstract class BaseWidget implements Widget{ private String name; public BaseWidget(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
abstract class CompoundWidget extends BaseWidget { protected List<Widget> children = new ArrayList<>(); public CompoundWidget(String name, Widget... widgets) { super(name); add(widgets); } public void add(Widget widget) { this.children.add(widget); } public void add(Widget...widgets){ this.children.addAll(Arrays.asList(widgets)); } public void remove(Widget widget) { this.children.remove(widget); } public void remove(Widget...widgets){ this.children.removeAll(Arrays.asList(widgets)); } @Override public void render() { for (Widget widget : children) { widget.render(); } }}
复制代码

CompoundWidget 实现类

class WinForm extends CompoundWidget{    public WinForm(String name, Widget... widgets) {        super(name, widgets);    }    @Override    public void render() {        System.out.println("print WinForm(" + this.getName() + ")");        super.render();    }}
class Frame extends CompoundWidget{ public Frame(String name, Widget... widgets) { super(name, widgets); } @Override public void render() { System.out.println("print Frame(" + this.getName() + ")"); super.render(); }}
复制代码

BaseWidget 实现 t 类

class Picture extends BaseWidget {
public Picture(String name) { super(name); }
@Override public void render() { System.out.println("print Picture(" + this.getName() + ")"); }}
class Button extends BaseWidget {
public Button(String name) { super(name); }
@Override public void render() { System.out.println("print Button(" + this.getName() + ")"); }}
class Label extends BaseWidget {
public Label(String name) { super(name); }
@Override public void render() { System.out.println("print Label(" + this.getName() + ")"); }}
class TextBox extends BaseWidget {
public TextBox(String name) { super(name); }
@Override public void render() { System.out.println("print TextBox(" + this.getName() + ")"); }}
class PasswordBox extends BaseWidget {
public PasswordBox(String name) { super(name); }
@Override public void render() { System.out.println("print PasswordBox(" + this.getName() + ")"); }}
class CheckBox extends BaseWidget {
public CheckBox(String name) { super(name); }
@Override public void render() { System.out.println("print CheckBox(" + this.getName() + ")"); }}
class LinkLabel extends BaseWidget {
public LinkLabel(String name) { super(name); }
@Override public void render() { System.out.println("print LinkLabel(" + this.getName() + ")"); }}
复制代码


用户头像

kk

关注

还未添加个人签名 2018.04.25 加入

还未添加个人简介

评论

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