Java设计模式架构算法设计模式和编程理论设计模式

设计模式之组合模式

2018-03-16  本文已影响119人  爱红旗渠

组合模式(Composite),将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

适用场景

需求中是体现部分与整体层次的结构是,以及你希望用户可以忽略组合对象与单个对象的不通过,统一地使用组合结构中的所有对象时,就应该考虑用组合模式了。

优点

  1. 高层模块调用简单。一棵树形机构中的所有节点都是Component,局部和整体对调用者来说没有任何区别,也就是说,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。
  2. 节点自由增加。使用了组合模式后,增加一个树枝节点、树叶节点变得非常容易,只要找到它的父节点就成,非常容易扩展,符合开闭原则,对以后的维护非常有利。

缺点

组合模式有一个非常明显的缺点,看到我们在客户端调用中的定义,提到树叶和树枝使用时的定义了吗?直接使用了实现类!这在面向接口编程上是很不恰当的,与依赖倒置原则冲突,读者在使用的时候要考虑清楚,它限制了你接口的影响范围。

UML结构图

CompositePattern

源码

Component:组合对象抽象类

/**
 * 组合对象抽象类
 *
 * Created by zhenguo on 11/30/14.
 */
public abstract class Component {

    protected String name;

    public Component(String name) {
        this.name = name;
    }

    public abstract void add(Component component);

    public abstract void remove(Component component);

    public abstract void display(int depth);

    protected String getDepthStr(int depth) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("-");
        }
        return sb.toString();
    }

}

Leaf:叶子节点,无子节点

/**
 * 叶子节点,无子节点
 *
 * Created by zhenguo on 11/30/14.
 */
public class Leaf extends Component {

    public Leaf(String name) {
        super(name);
    }

    @Override
    public void add(Component component) {
        System.out.println("Cannot add to a leaf!");
    }

    @Override
    public void remove(Component component) {
        System.out.println("Cannot remove to a leaf!");
    }

    @Override
    public void display(int depth) {
        System.out.println(getDepthStr(depth) + name);
    }

}

Composite:子节点

/**
 * 子节点
 *
 * Created by zhenguo on 11/30/14.
 */
public class Composite extends Component {

    private List<Component> children = new ArrayList<Component>();

    public Composite(String name) {
        super(name);
    }

    @Override
    public void add(Component component) {
        children.add(component);
    }

    @Override
    public void remove(Component component) {
        children.remove(component);
    }

    @Override
    public void display(int depth) {
        System.out.println(getDepthStr(depth) + name);
        for (Component component : children) {
            component.display(depth + 2);
        }
    }
}

Client:客户端调用

/**
 * 客户端调用
 * 组合模式(Composite),将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户
 * 对单个对象和组合对象的使用具有一致性。
 *
 * Created by zhenguo on 11/30/14.
 */
public class Client {

    public static void main(String[] args) {
        Composite root = new Composite("root");
        root.add(new Leaf("Leaf A"));
        root.add(new Leaf("Leaf B"));

        Composite composite = new Composite("Composite X");
        composite.add(new Leaf("Leaf XA"));
        composite.add(new Leaf("Leaf XB"));

        root.add(composite);

        Composite composite1 = new Composite("Composite XY");
        composite1.add(new Leaf("Leaf XYA"));
        composite1.add(new Leaf("Leaf XYB"));

        composite.add(composite1);

        root.add(new Leaf("Leaf C"));

        Leaf leaf = new Leaf("Leaf D");
        root.add(leaf);
        root.remove(leaf);

        root.display(1);
    }

}

设计模式系列:

创建型 结构型 行为型
工厂方法
简单工厂
抽象工厂
建造者模式
单例模式
原型模式
组合模式
外观模式
桥接模式
代理模式
享元模式
适配器模式
装饰模式
策略模式
模板方法
状态模式
观察者模式
备忘录模式
迭代器模式
命令模式
职责链模式
解释器模式
访问者模式
中介者模式
上一篇 下一篇

猜你喜欢

热点阅读