长生栈 长生栈
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Living Team

编程技术分享
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 创建型模式

  • 结构型模式

    • 适配器模式(Adapter Pattern)
    • 桥接模式(Bridge Pattern)
    • 组合模式(Composite Pattern)
      • 概念
      • 思想
      • 角色
      • 优点
      • 缺点
      • 类图
      • 示例代码
    • 装饰器模式(Decorator Pattern)
    • 外观模式(Facade Pattern)
    • 享元模式(Flyweight Pattern)
    • 代理模式(Proxy Pattern)
  • 行为型模式

  • 设计模式
  • 结构型模式
DC Wang
2023-05-28
目录

组合模式(Composite Pattern)

# 组合模式(Composite Pattern)

# 概念

组合模式(Composite Pattern)是一种结构型设计模式,它允许将对象组合成树形结构以表示"整体-部分"的层次结构。组合模式使得客户端可以统一对待单个对象和组合对象,从而使得客户端代码更加简单且可扩展。

# 思想

组合模式的思想是通过将对象组合成树状结构,使得客户端可以统一地对待单个对象和组合对象。每个组合对象都可以拥有其他组合对象作为其子对象,从而形成递归的结构。

# 角色

  1. Component(组件):定义组合中的对象接口,可以为叶节点和组合节点提供一致的操作。
  2. Leaf(叶节点):表示组合中的叶子对象,它没有子对象。
  3. Composite(组合节点):表示组合中的复杂对象,它可以包含其他组合对象作为子对象。

# 优点

  1. 简化客户端代码:客户端无需区分处理单个对象和组合对象,统一调用组件接口即可。
  2. 增强灵活性:可以在不修改现有代码的情况下,增加新的组件类型。
  3. 可以递归地组合对象:组合模式支持无限级的嵌套结构,可以方便地处理树形结构数据。

# 缺点

  1. 限制组件类型:由于组件接口要求统一的操作,可能会限制某些特定操作,需要进行类型判断或转换。
  2. 增加系统复杂性:引入组合模式会增加系统的类和对象数量,增加系统的理解和维护成本。

# 类图

下面是使用PlantUML绘制的组合模式的类图:

@startuml

class Component {
    + operation(): void
    + add(component: Component): void
    + remove(component: Component): void
    + getChild(index: int): Component
}

class Leaf {
    + operation(): void
}

class Composite {
    + operation(): void
    + add(component: Component): void
    + remove(component: Component): void
    + getChild(index: int): Component
}

note "Component" as note_component {
    The interface that defines operations for both leaf and composite objects.
}

note "Leaf" as note_leaf {
    Represents a leaf object that has no children.
}

note "Composite" as note_composite {
    Represents a composite object that can contain other components.
}

Client -- Component
Component <|-- Leaf
Component <|-- Composite
Composite "*" -- "0..n" Component : children

@enduml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# 示例代码

下面是一个简单的示例代码,展示了如何使用组合模式来处理文件系统的层次结构。在这个示例中,Component 表示文件系统中的对象,Leaf 表示文件,Composite 表示文件夹。

#include <iostream>
#include <vector>
#include <algorithm>

class Component {
public:
    virtual void operation() const = 0;
    virtual void add(Component* component) {}
    virtual void remove(Component* component) {}
    virtual Component* getChild(int index) { return nullptr; }
};

class Leaf : public Component {
public:
    void operation() const override {
        std::cout << "Leaf operation" << std::endl;
    }
};

class Composite : public Component {
private:
    std::vector<Component*> children;

public:
    void operation() const override {
        std::cout << "Composite operation" << std::endl;
        for (auto child : children) {
            child->operation();
        }
    }

    void add(Component* component) override {
        children.push_back(component);
    }

    void remove(Component* component) override {
        // Find and remove the component
        auto it = std::find(children.begin(), children.end(), component);
        if (it != children.end()) {
            children.erase(it);
        }
    }

    Component* getChild(int index) override {
        if (index >= 0 && index < children.size()) {
            return children[index];
        }
        return nullptr;
    }
};

int main() {
    Component* root = new Composite();
    Component* folder1 = new Composite();
    Component* folder2 = new Composite();
    Component* file1 = new Leaf();
    Component* file2 = new Leaf();
    Component* file3 = new Leaf();

    folder1->add(file1);
    folder1->add(file2);
    folder2->add(file3);
    root->add(folder1);
    root->add(folder2);

    root->operation();

    delete root;
    delete folder1;
    delete folder2;
    delete file1;
    delete file2;
    delete file3;

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

代码运行结果:

Composite operation
Composite operation
Leaf operation
Leaf operation
Composite operation
Leaf operation
1
2
3
4
5
6

以上示例代码展示了如何使用组合模式来处理文件系统的层次结构。组合模式将文件和文件夹统一表示为 Component 接口,客户端可以通过调用 operation 方法来对整个文件系统进行操作。运行结果显示了组合对象的递归操作,首先执行了 Composite 的 operation 方法,然后依次执行了 Leaf 的 operation 方法。

编辑 (opens new window)
#设计模式#结构型模式
上次更新: 2023/06/10, 08:05:09
桥接模式(Bridge Pattern)
装饰器模式(Decorator Pattern)

← 桥接模式(Bridge Pattern) 装饰器模式(Decorator Pattern)→

最近更新
01
ESP32-网络摄像头方案
06-14
02
ESP32-PWM驱动SG90舵机
06-14
03
ESP32-实时操作系统freertos
06-14
更多文章>
Theme by Vdoing | Copyright © 2019-2025 DC Wang All right reserved | 辽公网安备 21021102001125号 | 吉ICP备20001966号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式