架构学习第三周作业
发布于: 2020 年 06 月 25 日
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。
2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
package com.hy.componet;import java.util.ArrayList;abstract class Component { public void doSome() { throw new UnsupportedOperationException("不支持的操作"); }; public void addChild(Component child){ throw new UnsupportedOperationException("不支持的操作"); };}class ComponentNode extends Component { private String name; private ArrayList<Component> childs = null; public ComponentNode(String name) { this.name = name; } public void addChild(Component child) { if (null == childs) { this.childs = new ArrayList<Component>(); } this.childs.add(child); } public void doSome() { System.out.printf("print %s(%s)\r\n", this.getClass().getSimpleName(), this.name); for (Component child: childs) { child.doSome(); } }}class ComponentLeaf extends Component { private String name; public ComponentLeaf(String name) { this.name = name; } public void doSome() { System.out.printf("print %s(%s)\r\n", this.getClass().getSimpleName(), this.name); }}class WinForm extends ComponentNode { public WinForm(String name) { super(name); }}class Picture extends ComponentLeaf { public Picture(String name) { super(name); }}class Frame extends ComponentNode { public Frame(String name) { super(name); }}class Button extends ComponentLeaf { public Button(String name) { super(name); }}class Lable extends ComponentLeaf { public Lable(String name) { super(name); }}class TextBox extends ComponentLeaf { public TextBox(String name) { super(name); }}class PasswordBox extends ComponentLeaf { public PasswordBox(String name) { super(name); }}class CheckBox extends ComponentLeaf { public CheckBox(String name) { super(name); }}class LinkLable extends ComponentLeaf { public LinkLable(String name) { super(name); }}public class Main { public static void main(String[] args) { WinForm form = new WinForm("WINDOW窗口"); Picture pic = new Picture("LOGO图片"); Button btnLogin = new Button("登陆"); Button btnRegister = new Button("注册"); Frame frame = new Frame("FRAME1"); Lable userNameLable = new Lable("用户名"); TextBox textBox = new TextBox("文本框"); Lable passwordLable = new Lable("密码"); PasswordBox passwordBox = new PasswordBox("密码框"); CheckBox checkBox = new CheckBox("复选框"); TextBox recordUserBox = new TextBox("记住用户名"); LinkLable linkLable = new LinkLable("忘记密码"); form.addChild(pic); form.addChild(btnLogin); form.addChild(btnRegister); form.addChild(frame); frame.addChild(userNameLable); frame.addChild(textBox); frame.addChild(passwordLable); frame.addChild(passwordBox); frame.addChild(checkBox); frame.addChild(recordUserBox); frame.addChild(linkLable); form.doSome(); }}
划线
评论
复制
发布于: 2020 年 06 月 25 日阅读数: 50
版权声明: 本文为 InfoQ 作者【乐天】的原创文章。
原文链接:【http://xie.infoq.cn/article/5ca90ef3d04510a8dfdd2ed65】。未经作者许可,禁止转载。
乐天
关注
还未添加个人签名 2020.02.02 加入
还未添加个人简介
评论