zoukankan      html  css  js  c++  java
  • 设计模式:组合模式

    定义

    组合模式,他的宗旨是通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示,使得客户对单个对象和组合对象的使用具有一致性,属于结构型设计模式。

    组合模式一般用于描述整体与部分的关系,它将对象组织到树形结构中,顶层的节点被称为根节点,根节点下面可以包含树枝节点和叶子节点,树枝节点下面又可以包含树枝节点和叶子节点,结构如下:

    image-20210101201148310

    使用场景:

    • 希望客户端可以忽略组合对象与单个对象的差异
    • 对象层次具备整体和部分,呈树形结构

    透明组合模式通用写法

    透明组合模式是把所有的公共方法都定义在父类中,这样的好处是客户端无需分辨叶子节点和树枝节点,都具有完全一致的接口。

    public abstract class Component {
    
        protected String name;
    
        public abstract String operate();
    
        public Component(String name) {
            this.name = name;
        }
    
        public boolean addChild(Component component){
            throw new UnsupportedOperationException();
        }
    
        public boolean removeChild(Component component){
            throw new UnsupportedOperationException();
        }
    
        public Component getChild(int index){
            throw new UnsupportedOperationException();
        }
    }
    
    //树枝节点
    public class Composite extends Component{
    
        private List<Component> components = new ArrayList<>();
    
        @Override
        public String operate() {
            StringBuilder sb = new StringBuilder(this.name);
            for (Component component : components) {
                sb.append("
    ");
                sb.append(component.operate());
            }
            return sb.toString();
        }
    
        public Composite(String name) {
            super(name);
        }
    
        @Override
        public boolean addChild(Component component) {
            return components.add(component);
        }
    
        @Override
        public boolean removeChild(Component component) {
            return components.remove(component);
        }
    
        @Override
        public Component getChild(int index) {
            return components.get(index);
        }
    }
    
    //叶子节点
    public class Leaf extends Component{
    
        public Leaf(String name){
            super(name);
        }
    
        @Override
        public String operate() {
            return this.name;
        }
    }
    

    测试:

    public class Test {
    
        public static void main(String[] args) {
            Component a = new Composite("A");
            Component b = new Composite("B");
            Component c = new Composite("C");
            Component d = new Leaf("D");
            Component e = new Leaf("E");
    
            a.addChild(b);
            a.addChild(d);
    
            b.addChild(c);
            c.addChild(e);
    
            String operate = a.operate();
            System.out.println(operate);
        }
    }
    

    uml类图

    image-20210101203505497

    安全组合模式写法

    安全组合模式之规定系统各个层次的最基础的一致行为,而把组合(节点)本身的方法放到自身中,可以避免叶子节点引入冗余方法。

    需要修改父类

    public abstract class Component {
    
        protected String name;
    
        public abstract String operate();
    
        public Component(String name) {
            this.name = name;
        }
    
    }
    
  • 相关阅读:
    虚拟机安装Ubuntu 18.04.1 LTS教程
    Ubuntukylin-16.04.4设置root用户自动登陆
    git 删除本地分支,远程分支,创建tag
    elementui多选后无法再选择或者取消
    git分支改名oldName改为newName
    js前端流的方式下载execl
    vue实现网页导出pdf
    vue下载图片
    js原生方法 document.execCommand实现复制
    js原生方法 document.execCommand实现复制
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/14220821.html
Copyright © 2011-2022 走看看