组合模式打印窗口组件的树结构
发布于: 2020 年 06 月 22 日
1、组合部件
package com.cyc.demo.composite;/** * 组合部件 * @author CHENYANCHUN */public abstract  class Component {    /**     * 操作接口     * @param printStr     */    public abstract void operation(String printStr);    public void addChild(Component child){        throw new UnsupportedOperationException("对象不支持此功能");    }    public void removeChild(Component child){        throw new UnsupportedOperationException("对象不支持此功能");    }}2、枝类
package com.cyc.demo.composite;import org.springframework.util.CollectionUtils;import java.util.ArrayList;import java.util.List;/** * @author CHENYANCHUN * 枝类 */public class Composite extends Component{    private String name;    private List<Component> childComponents = new ArrayList<>();    public Composite(String name){        this.name = name;    }    @Override    public void operation(String printStr) {        System.out.println(printStr + " " + name);        if(!CollectionUtils.isEmpty(childComponents)){            childComponents.forEach(x -> {                x.operation(printStr);            });        }    }    @Override    public void addChild(Component child){        childComponents.add(child);    }    @Override    public void removeChild(Component child){        childComponents.remove(child);    }}
3、叶子类
package com.cyc.demo.composite;/** * @author CHENYANCHUN * 叶子类 */public class Leaf extends Component{    private String name;    public Leaf(String name){        this.name =name;    }    @Override    public void operation(String printStr) {        System.out.println(printStr + " " + name);    }}
4、输出窗口组件
package com.cyc.demo.composite;/** * @author CHENYANCHUN * 输出窗口组件 */public class ComponentPrint {    public static void main(String[] args) {        Composite winform = new Composite("WinForm");        Component picture = new Leaf("Picture");        Component loginButton = new Leaf("LoginButton");        Component signUpButton = new Leaf("SignUpButton");        Composite frame = new Composite("Frame");        Component userNameLable = new Leaf("UserNameLable");        Component textBox = new Leaf("TextBox");        Component passwordLable = new Leaf("PasswordLable");        Component passwordBox = new Leaf("PasswordBox");        Component checkBox = new Leaf("CheckBox");        Component rememberTextBox = new Leaf("RememberTextBox");        Component linkLable = new Leaf("LinkLable");        frame.addChild(userNameLable);        frame.addChild(textBox);        frame.addChild(passwordLable);        frame.addChild(passwordBox);        frame.addChild(checkBox);        frame.addChild(rememberTextBox);        frame.addChild(linkLable);        winform.addChild(picture);        winform.addChild(loginButton);        winform.addChild(signUpButton);        winform.addChild(frame);        winform.operation("print");    }}
5、输出结果
print WinFormprint Pictureprint LoginButtonprint SignUpButtonprint Frameprint UserNameLableprint TextBoxprint PasswordLableprint PasswordBoxprint CheckBoxprint RememberTextBoxprint LinkLable
正文少于50字补充:
组合模式允许你将对象组合成树形结构来表现”部分-整体“的层次结构,使得客户以一致的方式处理单个对象以及对象的组合。
组合模式实现的最关键的地方是——简单对象和复合对象必须实现相同的接口。这就是组合模式能够将组合对象和简单对象进行一致处理的原因。
- 组合部件(Component):它是一个抽象角色,为要组合的对象提供统一的接口。 
- 叶子(Leaf):在组合中表示子节点对象,叶子节点不能有子节点。 
- 合成部件(Composite):定义有枝节点的行为,用来存储部件,实现在Component接口中的有关操作,如增加(Add)和删除(Remove)。 
 划线
   评论
  复制
发布于: 2020 年 06 月 22 日 阅读数: 40
 
 橙
  关注 
everything will be alright 2020.04.06 加入
还未添加个人简介
 
 
  
  
 
 
 
  
  
  
  
  
  
  
  
    
评论