写点什么

架构训练营第三周作业

用户头像
李日盛
关注
发布于: 2020 年 11 月 08 日
架构训练营第三周作业
  1. 手写一个单例实现



  1. 基于组合模式,打印 windows 窗口树结构


类的 UML 结构图如下:



输出结果如下:



代码实现:


接口类:Component

package week3;
/** * @Description: 定义组件的基本行为 * @Author: easonlee * @CreateDate 2020/11/8 */public interface Component {
/*** * 打印自身和子组件内容 */ void print();
/*** * 增加子节点 * @param child */ void addChild(Component child);
/*** * 删除子节点 * @param child */ void removeChild(Component child);
}
复制代码


包含子节点的实现类:ContainerComponent


package week3;
import java.util.HashSet;import java.util.Set;
/** * @Description: TODO * @Author: easonlee * @CreateDate 2020/11/8 */public class ContainerComponent implements Component{
private String name; private String title; private Set<Component> childElements;
public ContainerComponent(String name, String title){ this.name = name; this.title = title; childElements = new HashSet<>(); }

@Override public void print() { StringBuffer content = new StringBuffer(); content.append("+"); content.append(" ").append(name).append(" ").append("(").append(title).append(")"); System.out.println(content); for (Component c:childElements){ c.print(); } }
@Override public void addChild(Component child) { childElements.add(child); }
@Override public void removeChild(Component child) { childElements.remove(child); }}
复制代码


不含子节点的实现类:SingleComponent

package week3;
/** * @Description: TODO * @Author: easonlee * @CreateDate 2020/11/8 */public class SingleComponent implements Component{
private String name; private String title;
public SingleComponent(String name, String title){ this.name = name; this.title = title; }
@Override public void print() { StringBuffer content = new StringBuffer(); content.append(" ").append("-"); content.append(" ").append(name).append(" ").append("(").append(title).append(")"); System.out.println(content); }
@Override public void addChild(Component child) { throw new UnsupportedOperationException("对象不支持此功能"); }
@Override public void removeChild(Component child) { throw new UnsupportedOperationException("对象不支持此功能"); }}
复制代码


运行的 main 类:ComponentMainDemo

package week3;

/** * @Description: TODO * @Author: easonlee * @CreateDate 2020/11/8 */public class ComponentMainDemo {
public static void main(String[] args) { Component root = new ContainerComponent("WinForm","WINDOWS窗口"); Component logo = new SingleComponent("Picture","LOGO图片");
Component loginButton = new SingleComponent("Button","登录"); Component regisButton = new SingleComponent("Button","注册"); Component inputFrame = new ContainerComponent("Frame","FRAME1"); Component userNameLabel = new SingleComponent("Label","用户名"); Component userNameInput = new SingleComponent("TextBox","文本框"); Component passwordLabel = new SingleComponent("Label","密码"); Component passwordInput = new SingleComponent("PasswordBox","密码框"); Component rememberCheck = new SingleComponent("CheckBox","复选框"); Component remember = new SingleComponent("TextBox","记住用户名"); Component forgetLabel = new SingleComponent("PrintLabel","忘记密码");
root.addChild(logo); root.addChild(loginButton); root.addChild(regisButton); root.addChild(inputFrame);
inputFrame.addChild(userNameLabel); inputFrame.addChild(userNameInput); inputFrame.addChild(passwordLabel); inputFrame.addChild(passwordInput); inputFrame.addChild(rememberCheck); inputFrame.addChild(remember); inputFrame.addChild(forgetLabel);
root.print();

}}
复制代码


发布于: 2020 年 11 月 08 日阅读数: 57
用户头像

李日盛

关注

好架构=低成本+可实现 2018.01.22 加入

还未添加个人简介

评论 (1 条评论)

发布
用户头像
设计过程思路清晰
2020 年 11 月 15 日 19:29
回复
没有更多了
架构训练营第三周作业