• 设计模式:组合模式


    定义

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

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

    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;
        }
    
    }
    
  • 相关阅读:
    python易混易乱(2)
    python易混易乱(1)
    #1062 – Duplicate entry ‘1’ for key ‘PRIMARY’
    关于 flask 实现数据库迁移以后 如何根据创建的模型类添加新的表?
    Linux同步互斥(Peterson算法,生产者消费者模型)
    正则表达式(Python)
    进程间通信
    CSS常见简写规则整理
    Django Model
    Django杂记
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/14220821.html
Copyright © 2020-2023  润新知