设计模式之组合模式详解

设计模式之组合模式详解

一、什么是组合模式

组合模式(Composite Pattern) 也称为整体-部分模式(Part-Whole) ,它的宗旨是通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示,使得客户对单个对象和组合对象的使用具有一致性,属于结构型模式。

二、组合模式的角色组成

  1. 抽象根节点(Component): 定义系统各层次对象的共有方法和属性,可以预先定义一些默认行为和属性;
  2. 树枝节点(Composite): 定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构;
  3. 叶子结点(Leaf): 叶子节点对象,其下再无分支,是系统层次遍历的最小单位
    在这里插入图片描述
    从上图可以看出,其实根节点和树枝节点本质上是同一种数据类型,可以作为容器使用;而叶子节点与树枝节点在语义上不属于同一种类型,但是在组合模式中,会把树枝节点和叶子节点认为是同一种数据类型(用同一接口定义),让他们具备一致行为。这样在组合模式中,整个树形结构中的对象都是同一种类型,带来的好处就是客户无需辨别树枝节点还是叶子节点,而是可以直接进行操作,给客户使用带来极大的便利。

三、组合模式应用场景

  1. 希望客户端可以忽略组合对象与单个对象的差异时
  2. 对象层次具备整体和部分,呈树形结构

我们生活中组合模式也很常见,比如树形菜单,操作系统目录结构,公司组织架构等

四、组合模式–透明组合模式示例

透明组合模式是把所有公共方法都定义在Component中,这样做的好处是客户端无需分辨是叶子节点(Leaf)和树枝节点(Composite),他们具备完全一致的接口。缺点是叶子节点(Leaf)会继承一些它所不需要(管理子类操作的方法)的方法,违背了接口隔离原则。

先创建一个抽象根节点Component对象:

/**
 * 抽象根节点
 */
public abstract class Component {
    protected String name;

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

    public abstract String operation();

    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(int index) {
        throw new UnsupportedOperationException("getChild not supported!");
    }
}

创建树枝节点Composite :

/**
 * 树枝节点
 */
public class Composite extends Component {

    private List<Component> mComponents;

    public Composite(String name) {
        super(name);
        this.mComponents = new ArrayList<Component>();
    }
    
    @Override
    public String operation() {
        StringBuilder builder = new StringBuilder(this.name);
        for (Component component : this.mComponents) {
            builder.append("\n");
            builder.append(component.operation());
        }
        return builder.toString();
    }
    
    @Override
    public boolean addChild(Component component) {
        return this.mComponents.add(component);
    }
    
    @Override
    public boolean removeChild(Component component) {
        return this.mComponents.remove(component);
    }
    
    @Override
    public Component getChild(int index) {
        return this.mComponents.get(index);
    }
}

创建叶子节点Leaf:

/**
 * 叶子节点
 */
public class Leaf extends Component {

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

    @Override
    public String operation() {
        return this.name;
    }
}

编写测试类:

public class Test {
    public static void main(String[] args) {
        // 来一个根节点
        Component root = new Composite("root");
        // 来一个树枝节点
        Component branchA = new Composite("\t\t--branchA");
        Component branchB = new Composite("\t\t\t\t------branchB");
        // 来一个叶子节点
        Component leafA = new Leaf("\t\t\t\t------leafA");
        Component leafB = new Leaf("\t\t\t\t\t\t-------leafB");
        Component leafC = new Leaf("\t\t--leafC");

        root.addChild(branchA);
        root.addChild(leafC);
        branchA.addChild(leafA);
        branchA.addChild(branchB);
        branchB.addChild(leafB);

        String result = root.operation();
        System.out.println(result);

    }
}

代码测试,控制台输出:
在这里插入图片描述

五、组合模式–安全组合模式示例

安全组合模式是只规定系统各个层次的最基础的一致行为,而把组合(树节点)本身的方法(管理子类对象的添加、删除等)放到自身当中。

创建抽象根节点Component对象:

/**
 * 抽象根节点
 */
public abstract class Component {

    protected String name;

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

    public abstract String operation();
}

创建树枝节点Composite :

/**
 * 树枝节点
 */
public class Composite extends Component {
    private List<Component> mComponents;

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

    @Override
    public String operation() {
        StringBuilder builder = new StringBuilder(this.name);
        for (Component component : this.mComponents) {
            builder.append("\n");
            builder.append(component.operation());
        }
        return builder.toString();
    }


    public boolean addChild(Component component) {
        return this.mComponents.add(component);
    }


    public boolean removeChild(Component component) {
        return this.mComponents.remove(component);
    }


    public Component getChild(int index) {
        return this.mComponents.get(index);
    }

}

创建叶子节点Leaf:

/**
 * 叶子节点
 */
public class Leaf extends Component {

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

    @Override
    public String operation() {
        return this.name;
    }
}

编写测试类:

public class Test {
    public static void main(String[] args) {
        // 来一个根节点
        Composite root = new Composite("root");
        // 来一个树枝节点
        Composite branchA = new Composite("\t\t--BranchA");
        Composite branchB = new Composite("\t\t\t\t----BranchB");
        // 来一个叶子节点
        Component leafA = new Leaf("\t\t\t\t----LeafA");
        Component leafB = new Leaf("\t\t\t\t\t\t------LeafB");
        Component leafC = new Leaf("\t\t--LeafC");
        root.addChild(branchA);
        root.addChild(leafC);
        branchA.addChild(leafA);
        branchA.addChild(branchB);
        branchB.addChild(leafB);
        String result = root.operation();
        System.out.println(result);
    }
}

代码测试,控制台输出:
在这里插入图片描述

六、组合模式优缺点

  • 优点
    • 清楚地定义分层次的复杂对象,表示对象的全部或部分层次
    • 让客户端忽略了层次的差异,方便对整个层次结构进行控制
    • 简化客户端代码
    • 符合开闭原则
  • 缺点
    • 限制类型时会较为复杂
    • 使设计变得更加抽象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值