美文网首页
组合模式及其应用

组合模式及其应用

作者: 文景大大 | 来源:发表于2022-06-21 12:00 被阅读0次

一、模式介绍

组合模式一般用来描述整体与部分的关系,特别适合树形结构的场景。在树形结构中,有三种角色:

  • 根节点,最顶层的节点;
  • 树枝节点,树枝节点往下可以包含树枝节点和叶子节点;
  • 叶子节点,最底层的节点;

组合模式旨在将叶子节点和树枝节点通过统一的接口进行表示,使得客户端在使用叶子节点和树枝节点时具有操作上的一致性。

1.1 透明组合模式

透明组合模式强调把所有公共方法都定义在抽象节点中,无论下面所有的叶子节点和树枝节点是否都需要使用这些公共方法。

public abstract class Component {
    protected String name;

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

    /**
     * 抽象方法等待子类实现
     */
    public abstract void operation();

    /**
     * 如下写法是为了不强制让子类重写这些方法
     * @param component
     * @return
     */
    public boolean addChild(Component component){
        throw new UnsupportedOperationException("addChild not supported!");
    }

    public boolean removeChild(Component component) {
        throw new UnsupportedOperationException("removeChild not supported!");
    }

    public Component getChild(String name){
        throw new UnsupportedOperationException("getChild not supported!");
    }
}
/**
 * 根节点、树枝节点
 */
@Slf4j
public class Composite extends Component{
    /**
     * 用来存放下层的树枝节点或者叶子节点
     */
    private List<Component> componentList;

    public Composite(String name) {
        super(name);
        this.componentList = new ArrayList<Component>();
    }

    @Override
    public void operation() {
        log.info("Composite:{} operation!", name);
        for (Component component : componentList) {
            component.operation();
        }
    }

    @Override
    public boolean addChild(Component component) {
        return this.componentList.add(component);
    }

    @Override
    public boolean removeChild(Component component) {
        return this.componentList.remove(component);
    }

    @Override
    public Component getChild(String name) {
        for (Component component : componentList) {
            if(name.equals(component.name)){
                return component;
            }
        }
        return null;
    }
}
/**
 * 叶子节点
 */
@Slf4j
public class Leaf extends Component{
    public Leaf(String name){
        super(name);
    }

    @Override
    public void operation() {
        log.info("Leaf:{} operation!", name);
    }
}
@Slf4j
public class Main {
    public static void main(String[] args) {
        /**
         * 新增根节点、树枝节点、叶子节点
         */
        Component root = new Composite("root");
        Component branch01 = new Composite("branch01");
        Component branch02 = new Composite("branch02");
        Component leaf0101 = new Leaf("leaf0101");
        Component leaf0201 = new Leaf("leaf0201");
        Component leaf0202 = new Leaf("leaf0202");

        /**
         * 构造树形结构
         */
        root.addChild(branch01);
        root.addChild(branch02);
        branch01.addChild(leaf0101);
        branch02.addChild(leaf0201);
        branch02.addChild(leaf0202);

        /**
         * 任意层级的节点操作上具有一致性
         */
        log.info("root操作!!!!!");
        root.operation();
        log.info("branch操作!!!!!");
        branch02.operation();
        log.info("leaf操作!!!!!");
        leaf0201.operation();
    }
}

1.2 安全组合模式

安全组合模式则是强调抽象节点只定义最基础的行为,各个树枝节点和叶子节点自己定义自己需要的行为。如此符合接口隔离原则、单一职责原则。但是缺点也十分明显,客户端在使用的时候,就需要区分树枝节点和叶子节点了,违反了依赖倒置原则。

public abstract class Component {
    protected String name;

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

    /**
     * 抽象方法等待子类实现
     */
    public abstract void operation();
}
@Slf4j
public class Main {
    public static void main(String[] args) {
        /**
         * 新增根节点、树枝节点、叶子节点
         */
        Composite root = new Composite("root");
        Composite branch01 = new Composite("branch01");
        Composite branch02 = new Composite("branch02");
        Component leaf0101 = new Leaf("leaf0101");
        Component leaf0201 = new Leaf("leaf0201");
        Component leaf0202 = new Leaf("leaf0202");

        /**
         * 构造树形结构
         */
        root.addChild(branch01);
        root.addChild(branch02);
        branch01.addChild(leaf0101);
        branch02.addChild(leaf0201);
        branch02.addChild(leaf0202);

        /**
         * 任意层级的节点操作上具有一致性
         */
        log.info("root操作!!!!!");
        root.operation();
        log.info("branch操作!!!!!");
        branch02.operation();
        log.info("leaf操作!!!!!");
        leaf0201.operation();
    }
}

二、使用案例

2.1 JDK中的HashMap

  • 抽象节点Map
  • 树枝节点HashMap
  • 叶子节点Node

2.2 MyBatis中的SqlNode

  • 抽象节点SqlNode
  • 树枝节点TextSqlNode、WhereSqlNode、IfSqlNode等等;

三、模式总结

透明组合模式将所有公共操作都定义在了抽象节点中,那么所有下层的树枝节点和叶子节点都具有这些操作行为,因此如果系统中大多数层次的节点都具备相同行为的时候就比较适合采用透明组合模式;而如果各个层次的节点差异性较多则比较推荐使用安全组合模式。

3.1 优点

  • 比较清晰地定义了各个层级地对象;
  • 客户端可以忽略各个层次的差异,他们在操作上对客户而言具有一致性;
  • 符合开闭原则;

3.2 缺点

  • 设计变得复杂,有更多的类;
  • 限制类型时会比较复杂;

相关文章

  • 组合模式及其应用

    一、模式介绍 组合模式一般用来描述整体与部分的关系,特别适合树形结构的场景。在树形结构中,有三种角色: 根节点,最...

  • 组合模式(统一叶子与组合对象)

    目录 从生活场景出发,映射组合模式 组合模式的理论概念 组合模式的实现 组合模式在源码中的应用 组合 “优于” 继...

  • 设计模式 | 组合模式及典型应用

    本文的主要内容: 介绍组合模式 示例 组合模式总结 源码分析组合模式的典型应用java.awt中的组合模式Java...

  • JavaScript原型与继承(二)

    本文所述内容: 由组合构造模式详解组合继承模式,及其问题所在,问题所产生的原因,解决问题的办法 合理的继承模式原理...

  • 代理模式及其应用

    一、代理模式简介 代理模式旨在为服务类和客户类之间插入一个代理类,由代理类为客户类进行服务的代理访问。同时也可以对...

  • 门面模式及其应用

    一、模式介绍 门面模式又称为外观模式,即提供一个统一的接口或者类,用来访问各个子系统、子模块中的一群接口。在日常开...

  • 组合模式

    组合模式 组合(Composite)是指使用组合和继承关系将聚合体及其组成元素分解成树状结构,以便客户端在不需要区...

  • Activity启动模式及其应用

    模式介绍 [1] standard 模式这是默认模式,每次激活Activity时都会创建Activity实例,并放...

  • 享元模式及其应用

    一、模式介绍 享元模式(Flyweight)是一种对象池的实现方式,就像我们用过的线程池、数据库连接池等一样,主要...

  • 桥接模式及其应用

    一、模式介绍 桥接模式可以将抽象部分和具体实现部分进行分离,使他们都可以独立地变化。它存在的主要目的是通过组合的方...

网友评论

      本文标题:组合模式及其应用

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