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

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

习题 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();
    }
}


程序输出结果:


目录
相关文章
|
11天前
|
机器学习/深度学习 算法 安全
隐私计算训练营第三讲-详解隐私计算的架构和技术要点
SecretFlow 是一个隐私保护的统一框架,用于数据分析和机器学习,支持MPC、HE、TEE等隐私计算技术。它提供设备抽象、计算图表示和基于图的ML/DL能力,适应数据水平、垂直和混合分割场景。产品层包括SecretPad(快速体验核心能力)和SecretNote(开发工具)。算法层涉及PSI、PIR、数据分析和联邦学习(水平、垂直、混合)。此外,SecretFlow还有YACL密码库和Kusica任务调度框架,Kusica提供轻量化部署、跨域通信和统一API接口。
92 0
|
8月前
|
消息中间件 缓存 NoSQL
|
9月前
|
消息中间件 存储 关系型数据库
极客时间架构实战营作业八
极客时间架构实战营作业八
117 0
|
9月前
|
消息中间件 Java 中间件
极客时间架构实战营作业六
极客时间架构实战营作业六
73 0
|
9月前
|
运维 关系型数据库 MySQL
极客时间架构实战营作业三
极客时间架构实战营作业三
102 0
|
7月前
|
资源调度 分布式计算 调度
Fink--3、Flink运行时架构(并行度、算子链、任务槽、作业提交流程)
Fink--3、Flink运行时架构(并行度、算子链、任务槽、作业提交流程)
|
9月前
|
容灾 网络协议
极客时间架构实战营模块 7 作业
极客时间架构实战营模块 7 作业
59 0
|
9月前
|
存储 缓存 负载均衡
极客时间架构实战营作业五
极客时间架构实战营作业五
91 0
|
9月前
|
存储 JSON NoSQL
极客时间架构实战营作业四
极客时间架构实战营作业四
80 0
|
9月前
极客时间架构实战营作业二
极客时间架构实战营作业二
56 0