设计模式之组合模式

作者: 爱红旗渠 | 来源:发表于2018-03-16 18:20 被阅读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);
    }

}

设计模式系列:

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

相关文章

  • Android设计模式——组合模式(七大结构型)

    1.组合模式介绍 组合模式(Composite Pattern)也称为部分整体模式,是七大结构型设计模式之...

  • 组合模式

    设计模式之组合模式 什么是组合模式? 组合模式允许你将对象组合成树形结构来表现”部分-整体“的层次结构,使得客户以...

  • 组合模式

    设计模式系列7--组合模式 《Objective-c 编程之道 iOS 设计模式解析》 - 组合模式 常见组合模式...

  • 设计模式

    设计模式之组合模式 最新在工作之余,看了看这个设计模式,关于设计模式有很多种。最最常见的就是这个MVC模式或者...

  • Android 组合模式(View与ViewGroup)

    Android 设计模式系列文章 Android 23种设计模式 前言 组合设计模式,又被称为部分整体模式。组合模...

  • 设计模式之组合模式

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

  • 设计模式之组合模式

    1. 定义 组合模式,又叫部分整体模式,用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用...

  • 设计模式之组合模式

    设计模式之组合模式 1.组合模式的定义 将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组...

  • 设计模式之组合模式

    组合模式的定义 组合模式(Composite Pattern)也叫合成模式,有时候也叫整体-部分模式,主要用来描述...

  • 设计模式之组合模式

    组合模式 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用...

网友评论

    本文标题:设计模式之组合模式

    本文链接:https://www.haomeiwen.com/subject/ygyrqftx.html