组合模式之应用

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

  • 根节点,最顶层的节点;

  • 树枝节点,树枝节点往下可以包含树枝节点和叶子节点;

  • 叶子节点,最底层的节点;

  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. 安全组合模式

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

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();
    }
}
  1. 案例

(1) JDK中的HashMap

  • 抽象节点Map

  • 树枝节点HashMap

  • 叶子节点Node

(2)MyBatis中的SqlNode

  • 抽象节点SqlNode

  • 树枝节点TextSqlNode、WhereSqlNode、IfSqlNode等等;

  1. 总结

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

  • 优点

比较清晰地定义了各个层级地对象;

客户端可以忽略各个层次的差异,他们在操作上对客户而言具有一致性;

符合开闭原则;

  • 缺点

设计变得复杂,有更多的类;

限制类型时会比较复杂;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chuxuezhe_987

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值