Week3 作业
发布于: 2020 年 06 月 24 日
请在草稿纸上手写一个单例模式的实现代码,拍照提交作业
 
 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3
package com.xw.composite;public abstract class Component {    private String name;    private String type;    private String id;
    public Component(String name) {        this.name = name;    }
    public Component(String type, String name) {        this.type = type;        this.name = name;    }    public String getName() {        return name;    }
    public String getType() {        return type;    }
    public String getId() {        return id;    }    protected abstract void print();
}package com.xw.composite;import java.util.List;import java.util.ArrayList;
public abstract class Container extends Component {    private List<Component> components = new ArrayList<Component>();
    public Container(String name) {        super(name);    }
    public void add(Component c) {        this.components.add(c);    }
    public void remove(Component c) {        this.components.remove(c);    }
    @Override    protected void print() {        System.out.println("print ".concat(this.getClass().getSimpleName().concat("(").concat(getName()).concat(")")));        for(Component c : components) {            c.print();        }    }}package com.xw.composite;
public class Widget extends Component {    public Widget(String type, String name) {        super(type, name);
    }
    @Override    public void print() {        System.out.println("print " + getType() + "(" + getName() + ")");    }}package com.xw.composite;
public class WinForm extends Container {    public WinForm(String name) {        super(name);    }
    public static void main(String[] args) {        WinForm winForm = new WinForm("Windwos窗口");        winForm.add(new Widget("Picture", "LOGO图片"));        winForm.add(new Widget("Button", "登陆"));        winForm.add(new Widget("Button", "注册"));
        WinForm frame = new WinForm("frame");        frame.add(new Widget("LABEL", "用户名"));        frame.add(new Widget("TEXTBOX", "文本框"));        frame.add(new Widget("LABEL", "密码"));        frame.add(new Widget("PasswordBox", "文本框"));        frame.add(new Widget("CheckBox", "复选框"));        frame.add(new Widget("TextBox", "记住用户名"));        frame.add(new Widget("LinkLabel", "忘记密码"));        winForm.add(frame);        winForm.print();
    }}
复制代码
 划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 57

熊威
关注
还未添加个人签名 2019.06.12 加入
还未添加个人简介











 
    
评论