组合模式及其应用

2022-06-21  本文已影响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

2.2 MyBatis中的SqlNode

三、模式总结

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

3.1 优点

3.2 缺点

上一篇 下一篇

猜你喜欢

热点阅读