• 组合模式(Composite Pattern)


    定义

    将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

    动机

    当你发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一的使用组合对象结构中的所有对象时,就应该考虑用组合模式了。

    UML类图

    在这里插入图片描述

    • Component抽象构建角色, 定义参加组合独享的共同方法和属性,可以定义一些默认的行为或者属性
    • Leaf叶子结构,旗下没有分支,是最小的遍历单位
    • Composite树枝结构,作用是组合树枝节点和叶子结点形成的树形结构

    优点

    高层模块调用简单,所有结点都是Component ,局部和整体对调用者来说没有任何区别,高层不必关心自己处理的是单个对象还是整个组合结构。
    结点自由增加,使用组合模式,如果想增加树枝节点,树叶结点都很容易,只要找到父节点就行,符合开闭原则。

    缺点

    树枝和树叶结点使用时的定义,直接使用的是实现类。违背依赖倒置原则。

    使用场景

    维护和展示一部分-整体关系的场景:eg树形菜单,文件和文件夹管理。
    从一个整体中能够独立出部分模块和功能的场景。

    注意事项

    只要是树形结构,就考虑组合模式,只要体现这你部分关系,就使用组合模式
    Composite 模式在实现中有一个问题就是要提供对于子节点( Leaf)的管理策略,这里使用的是 STL 中的 vector,可以提供其他的实现方式,如数组、链表、 Hash 表等。

    示例

    在这里插入图片描述
    以一个大型公司为需求背景,组织我们的代码。一个北京总公司,下面有郑州和西安两个分公司。然后每个公司不管是总公司还是分公司都有自己的人力资源部,IT部和市场部。

    // component.h
    #ifndef COMPONENT_H
    #define COMPONENT_H
    #include <QList>
    
    class Component
    {
    public:
        Component(QString name);
        virtual ~Component();
        virtual void add(Component* com, int depth = 0) = 0;
        virtual void remove(Component* com) = 0;
        virtual void display() = 0;
        virtual void LineOfDuty() = 0;
    
        QString name();
        void setDepth(int depth);
        int depth();
    private:
        QString         m_Name;
        int                 m_Depth;
    };
    
    class ConcreteCompany : public Component
    {
    public:
        ConcreteCompany(QString name);
        virtual ~ConcreteCompany();
        virtual void add(Component* com, int depth = 0);
        virtual void remove(Component* com);
        virtual void display();
        virtual void LineOfDuty();
    private:
        QList<Component*> m_ChildCom;
    };
    
    class HumanResource : public Component
    {
    public:
        HumanResource(QString name);
        virtual ~HumanResource();
        virtual void add(Component* com, int depth = 0);
        virtual void remove(Component* com);
         virtual void display();
        virtual void LineOfDuty();
    };
    
    class IT : public Component
    {
    public:
        IT(QString name);
        virtual ~IT();
        virtual void add(Component* com, int depth = 0);
        virtual void remove(Component* com);
         virtual void display();
        virtual void LineOfDuty();
    };
    
    class Marketing : public Component
    {
    public:
        Marketing(QString name);
        virtual ~Marketing();
        virtual void add(Component* com, int depth = 0);
        virtual void remove(Component* com);
         virtual void display();
        virtual void LineOfDuty();
    };
    #endif // COMPONENT_H
    
    
    • 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
    // component.cpp
    /************************************
        * @brief	: 安排一下故事背景:有一个王者农药全国总公司在深圳,现在想要在全国开办事处
        * 1.北京办事处- 招聘部,研发部,市场部
        * 2.郑州办事处- 招聘部,研发部,市场部
        * 3.西安办事处- 招聘部,研发部,市场部
        * and so on...
        * @author	:   wzx
        * @date	:   2020-05-11
        * @project	:  Composite
    *************************************/
    #include <QDebug>
    #include "component.h"
    
    #define DELETEOBJECT(x) if(x) { delete x; x = nullptr; }
    
    Component::Component(QString name):m_Name(name) {}
    
    Component::~Component(){}
    
    QString Component::name()
    {
        return m_Name;
    }
    
    void Component::setDepth(int depth)
    {
        m_Depth = depth;
    }
    
    int Component::depth()
    {
        return m_Depth;
    }
    
    ConcreteCompany::ConcreteCompany(QString name)
        : Component(name)
    {
    
    }
    
    ConcreteCompany::~ConcreteCompany()
    {
        for(auto com : m_ChildCom)
        {
            DELETEOBJECT(com);
        }
        m_ChildCom.clear();
    }
    
    void ConcreteCompany::add(Component* com, int depth)
    {
        com->setDepth(depth);
        m_ChildCom.append(com);
    }
    
    void ConcreteCompany:: remove(Component* com)
    {
        m_ChildCom.removeOne(com);
    }
    
    void ConcreteCompany::display()
    {
        QString str;
        for(int n = 0; n < depth(); ++n)
            str += "--";
        qDebug() << qPrintable(str)  <<  (name());
        for(auto com : m_ChildCom)
        {
            com->display();
        }
    }
    
    void ConcreteCompany::LineOfDuty()
    {
    
    }
    
    HumanResource::HumanResource(QString name)
        : Component(name)
    {
    
    }
    
    HumanResource::~HumanResource()
    {
    
    }
    
    void HumanResource::add(Component* com, int depth)
    {
        com->setDepth(depth);
    }
    
    void HumanResource::remove(Component* com)
    {
    
    }
    
    void HumanResource::display()
    {
        QString str;
        for(int n = 0; n < depth(); ++n)
            str += "--";
        qDebug() << qPrintable(str)  <<  (name());
    }
    
    void HumanResource::LineOfDuty()
    {
        qDebug() << "人力资源部,负责招聘";
    }
    
    IT::IT(QString name)
        : Component(name)
    {
    
    }
    
    IT::~IT()
    {
    
    }
    
    void IT::add(Component* com, int depth)
    {
        com->setDepth(depth);
    }
    
    void IT::remove(Component* com)
    {
    
    }
    
    void IT::display()
    {
        QString str;
        for(int n = 0; n < depth(); ++n)
            str += "--";
        qDebug() << qPrintable(str)  <<  (name());
    }
    
    void IT::LineOfDuty()
    {
        qDebug() << "IT部门,负责写代码";
    }
    
    Marketing::Marketing(QString name)
        : Component(name)
    {
    
    }
    
    Marketing::~Marketing()
    {
    
    }
    
    void Marketing::add(Component* com, int depth)
    {
        com->setDepth(depth);
    }
    
    void Marketing::remove(Component* com)
    {
    
    }
    
    void Marketing::display()
    {
        QString str;
        for(int n = 0; n < depth(); ++n)
            str += "--";
        qDebug() << qPrintable(str)  <<  name();
    }
    
    void Marketing::LineOfDuty()
    {
        qDebug() << "市场部门,负责市场推广";
    }
    
    
    • 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
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    // main.cpp
    
    #include <QCoreApplication>
    #include <QDebug>
    #include "component.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        Component* root = new ConcreteCompany("北京总部");
        root->setDepth(0);
        root->add(new HumanResource("人力资源部门"), 1);
        root->add(new IT("IT部门"), 1);
        root->add(new Marketing("市场部门"), 1);
    
        Component* zz = new ConcreteCompany("郑州办事处");
        zz->add(new HumanResource("人力资源部门"), 2);
        zz->add(new IT("IT部门"), 2);
        zz->add(new Marketing("市场部门"), 2);
        root->add(zz, 1);
    
        Component* xa = new ConcreteCompany("西安办事处");
        xa->add(new HumanResource("人力资源部门"), 2);
        xa->add(new IT("IT部门"), 2);
        xa->add(new Marketing("市场部门"), 2);
        root->add(xa, 1);
    
        root->display();
        return a.exec();
    }
    
    
    • 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

    运行结果

    "北京总部"
    
    -- "人力资源部门"
    
    -- "IT部门"
    
    -- "市场部门"
    
    -- "郑州办事处"
    
    ---- "人力资源部门"
    
    ---- "IT部门"
    
    ---- "市场部门"
    
    -- "西安办事处"
    
    ---- "人力资源部门"
    
    ---- "IT部门"
    
    ---- "市场部门"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    示例二

    算术表达式包括操作数、操作符和另一个操作数,其中,另一个操作数也可以是操作数、操作符和另一个操作数。

  • 相关阅读:
    Zookeeper3.7.1分布式安装部署
    体验下,大厂在使用功能的API网关!
    详解Java算法之冒泡排序(Bubble Sorting)
    仪表盘样式百分比显示准确率等信息,通过旋转实现
    docker 常用命令 快捷命令
    【Kotlin】函数
    .net core 自定义授权策略提供程序进行权限验证
    遥感影像正射矫正及高分二号遥感影像获取
    SpringCloud Sleuth 分布式请求链路追踪
    拯救者y9000k(2022版)安装ubuntu系统(解决wifi问题)
  • 原文地址:https://blog.csdn.net/weixin_43956958/article/details/125449077