面向对象设计模式课程作业
发布于: 2020 年 06 月 24 日

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

作业二
问:请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。
package com.qiangjun;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * 窗口元素 * * @author leven */public class WindowElement {    /**     * 元素名称     */    private String name;    /**     * 元素类型     */    private String type;    /**     * 子元素     */    private List<WindowElement> children;    public WindowElement(String name, String type) {        this.name = name;        this.type = type;        children = new ArrayList<>();    }    public void addWindowElement(WindowElement windowElement) {        this.children.add(windowElement);    }    public void addWindowElements(WindowElement... windowElement){        this.children.addAll(Arrays.asList(windowElement));    }    public List<WindowElement> getChildren() {        return this.children;    }    public String print() {        return "print " + this.type + "(" + this.name + ")";    }}
package com.qiangjun;/** * 组合模式 * @author leven */public class CompositePatternDemo {    public static void main(String[] args) {        // create some windowElement        WindowElement winForm = new WindowElement("window窗口","WinForm");        WindowElement picture = new WindowElement("LOGO图片","Picture");        WindowElement login = new WindowElement("登录","Button");        WindowElement register = new WindowElement("注册","Button");        WindowElement frame = new WindowElement("FRAME1","Frame");        WindowElement username = new WindowElement("用户名","Label");        WindowElement textBox = new WindowElement("文本框","TextBox");        WindowElement passwordLabel = new WindowElement("密码","Label");        WindowElement passwordBox = new WindowElement("密码框","PasswordBox");        WindowElement checkBox = new WindowElement("复选框","CheckBox");        WindowElement remember = new WindowElement("记住用户名","TextBox");        WindowElement forgotPassword = new WindowElement("忘记密码","LinkLabel");        // set children        winForm.addWindowElements(picture,login,register,frame);        frame.addWindowElements(username,textBox,passwordLabel,passwordBox,checkBox,remember,forgotPassword);        //print window        System.out.println(winForm.print());        for (WindowElement windowElement : winForm.getChildren()) {            System.out.println(windowElement.print());            windowElement.getChildren().forEach(element -> {                System.out.println(element.print());            });        }    }}
 划线
   评论
  复制
发布于: 2020 年 06 月 24 日 阅读数: 37
版权声明: 本文为 InfoQ 作者【行下一首歌】的原创文章。
原文链接:【http://xie.infoq.cn/article/bf853edd16cf9c0dd9b20f810】。
本文遵守【CC BY-NC】协议,转载请保留原文出处及本版权声明。
行下一首歌
  关注 
还未添加个人签名 2017.10.30 加入
半壁山房待明月,一盏清茗酬知音。
 
 
  
  
 
 
 
 
 
 
 
 
 
 
    
评论