设计模式应用
发布于: 2020 年 06 月 24 日
1. 请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

2. 请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。

public interface Component {    public void print();    public void add(Component component);}public class Node implements Component{    private String nodeType;    private String nodeName;    ArrayList<Component> list = new ArrayList<Component>();    public Node(String nodeType,String nodeName){        this.nodeName = nodeName;        this.nodeType = nodeType;    }    @Override    public void add(Component component){        list.add(component);    }    @Override    public void print() {        System.out.println(this.nodeType + "(" + this.nodeName + ")");        for(Component component : list){            component.print();        }    }}public class Leaf implements Component {    private String leafType;    private String leafName;    public Leaf(String leafType, String leafName) {        this.leafType = leafType;        this.leafName = leafName;    }    @Override    public void print() {        System.out.println(this.leafType + "(" + this.leafName + ")");    }    @Override    public void add(Component component) {    }}public class Test {    public static void main(String[] args) {        Component windowForm = new Node("WinForm","WINDOW窗口");        Component picture = new Leaf("Picture","LOGO图片");        Component button1 = new Leaf("Button","登录");        Component button2 = new Leaf("Button","注册");        Component frame = new Node("Frame","FRAME1");        Component lable1 = new Leaf("Lable","用户名");        Component textBox1 = new Leaf("TextBox","文本框");        Component lable2 = new Leaf("Lable","密码");        Component passwordBox = new Leaf("PasswordBox","密码框");        Component checkBox = new Leaf("CheckBox","复选框");        Component textBox2 = new Leaf("TextBox","记住用户名");        Component linkLable = new Leaf("LinkLable","忘记密码");        frame.add(lable1);        frame.add(textBox1);        frame.add(lable2);        frame.add(passwordBox);        frame.add(checkBox);        frame.add(textBox2);        frame.add(linkLable);        windowForm.add(picture);        windowForm.add(button1);        windowForm.add(button2);        windowForm.add(frame);        windowForm.print();    }}> Task :Test.main()WinForm(WINDOW窗口)Picture(LOGO图片)Button(登录)Button(注册)Frame(FRAME1)Lable(用户�?)TextBox(文本�?)Lable(密码)PasswordBox(密码�?)CheckBox(复�?�框)TextBox(记住用户�?)LinkLable(忘记密码)
 划线
   评论
  复制
发布于: 2020 年 06 月 24 日 阅读数: 32
wei
  关注 
还未添加个人签名 2018.05.31 加入
还未添加个人简介
 
 
  
  
 
 
 
 
 
 
 
 
 
 
    
评论