设计模式之组合模式

本文介绍了组合模式(CompositePattern),它用于表示部分与整体关系。优点包括高层模块调用简洁,易于扩展;缺点是与面向接口编程冲突。提供了抽象类、组合类和叶子节点的示例代码,以及运行结果。
摘要由CSDN通过智能技术生成

定义

组合模式(Composite Pattern)也叫合成模式,有时又叫做部分-整体模式(Part-Whole),主要是用来描述部分与整体的关系,其定义如下:

Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.(将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。)
在这里插入图片描述

我们先来说说组合模式的几个角色:

  1. Component抽象构件角色

定义参加组合对象的共有方法和属性,可以定义一些默认的行为或属性,比如我们例子中的getInfo就封装到了抽象类中。

·Leaf叶子构件

叶子对象,其下再也没有其他的分支,也就是遍历的最小单位。

  1. Composite树枝构件

树枝对象,它的作用是组合树枝节点和叶子节点形成一个树形结构。

优缺点

优点

  1. 高层模块调用简单

    一棵树形机构中的所有节点都是Component,局部和整体对调用者来说没有任何区别,也就是说,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。

  2. 节点自由增加

    使用了组合模式后,我们可以看看,如果想增加一个树枝节点、树叶节点是不是都很容易,只要找到它的父节点就成,非常容易扩展,符合开闭原则,对以后的维护非常有利。

缺点

组合模式有一个非常明显的缺点,看到我们在场景类中的定义,提到树叶和树枝使用时的定义了吗?直接使用了实现类!这在面向接口编程上是很不恰当的,与依赖倒置原则冲突。

示例代码

  1. 抽象类组件
public abstract class Component {
    public void doSomething() {
        System.out.println("component do something");
    }
}
  1. 组合类

    public class Composite extends Component {
        // 构件容器
        private List<Component> componentArrayList = new ArrayList<Component>();
    
        // 增加一个叶子构件或树枝构件
        public void add(Component component) {
            this.componentArrayList.add(component);
        }
    
        // 删除一个叶子构件或树枝构件
        public void remove(Component component) {
            this.componentArrayList.remove(component);
        }
    
        // 获得分支下的所有叶子构件和树枝构件
        public List<Component> getChildren() {
            return this.componentArrayList;
        }
    }
    
  2. 叶子节点

    public class Leaf extends Component {
        @Override
        public void doSomething() {
            System.out.println("leaf do something");
        }
    }
    
  3. 测试方法

    @Test
    public void test() {
        // 创建一个根节点
        Composite root = new Composite();
        root.doSomething();
        // 创建一个树枝构件
        Composite branch = new Composite();
        // 创建一个叶子节点
        Leaf leaf = new Leaf();
        // 建立整体
        root.add(branch);
        branch.add(leaf);
        display(root);
    }
    
    // 通过递归遍历树
    public static void display(Composite root) {
        for (Component c : root.getChildren()) {
            if (c instanceof Leaf) { // 叶子节点
                c.doSomething();
            } else { // 树枝节点
                display((Composite)c);
            }
        }
    }
    

运行结果

component do something
leaf do something

示例代码地址

https://gitee.com/youxiaxiaomage/java-practices/tree/master/yxxmg-gof-sample/src/main/java/com/yxxmg/gof/structure/composite

  • 25
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

游侠小马哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值