练习 3-1
发布于: 2020 年 06 月 24 日
手写单例模式:

组合模式
组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户可以使用一致的方法操作单个对象和组合对象。
public class Window {    private String text = "";    public Window(String text) {        this.text = text;    }    public void print() {        System.out.println("print " + this.text);    }}
public class Container extends Window  {    private List<Window> children = new LinkedList<>();    public Container(String text) {        super(text);    }    public void addChild(Window child) {        this.children.add(child);    }    public void print() {        for(Window child:children){            child.print();        }    }}
public class Button extends Window  {    public Button(String text) {        super(text);    }}
public class CheckBox extends Window  {    public CheckBox(String text) {        super(text);    }}
public class Frame extends Container {    public Frame(String text) {        super(text);    }}
public class Lable extends Window  {    public Lable(String text) {        super(text);    }}
public static void main(String[] args) {  WinForm winForm = new WinForm("WinForm(WINDOW窗口)");  winForm.addChild(new Picture("Picture(LOG图片)"));  winForm.addChild(new Button("Button(登录)"));  winForm.addChild(new Button("Button(注册)"));  Frame frame = new Frame("FRAME(FRAME1)");  frame.addChild(new Lable("Lable(用户名)"));  frame.addChild(new TextBox("TextBox(文本框)"));  frame.addChild(new Lable("Lable(密码)"));  frame.addChild(new PasswordBox("PasswordBox(密码框)"));  frame.addChild(new CheckBox("CheckBox(复选框)"));  frame.addChild(new TextBox("TextBox(记住用户名)"));  frame.addChild(new LinkLable("LinkLable(忘记密码)"));  winForm.addChild(frame);  winForm.print();}输出结果:

 划线
   评论
  复制
发布于: 2020 年 06 月 24 日 阅读数: 26
闷骚程序员
  关注 
还未添加个人签名 2018.11.15 加入
还未添加个人简介
 
 
  
  
 
 
 
 
 
 
 
 
 
 
    
评论