C++设计模式之组合模式

C++设计模式之组合模式

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


一、缘由

在自然界中常常存在着许多树形关系,例如公司的结构,有子公司,部门。又如文件系统的结构,目录下边有目录或者文件,而目录下的目录又有目录和文件,如此递归下去。而组合模式就是为了处理这种树形关系而存在的。

二、实现

组合模式 天生就是为了表达树形关系的,树形关系的定义是递归的,故而组合模式的定义显然也是递归的。组合模式的UML类图如下:

在组合模式中存在三个角色:

  • Component 抽象构件类。该类给用户提供了一个统一的视角,用户可以对叶子和容器进行编程而无需在乎是叶子类或者是容器类。

  • Composite,容器构件类。容器构件类一方面可以提供操作如叶子类一样,另一方面有可以包含子构件,子构建可以是容易构件也可以是叶子类。如果用文件系统来类比,容易构件类就是目录。

  • Leaf,叶子类。叶子类可以提供功能操作,但是无法容纳子构件。如果用文件系统来类比,叶子节点就是普通文件。

三、实例代码

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

using std::string;

class Component {
public:
    virtual ~Component (){};
    virtual void operation() = 0;
    virtual void add(Component *subComponent){}
    virtual void remove(Component *subComponent){}
    virtual Component *getChild(std::vector<Component*>::size_type index){
        return NULL;
    }
};

class Leaf : public Component{
public:
    virtual ~Leaf(){};
    virtual void operation() override{
        std::cout << "Here is leaf" << std::endl;
    }
};

class Composite : public Component {
private:
    std::vector<Component*> children;
public:
    virtual ~Composite (){
        for(auto &child : children)
            delete child;
    };

    virtual void operation() override{
        std::cout << "Here is composite. childen : " << std::endl;
        for (auto &child : children){
            child->operation();
        }
    };

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

    virtual void remove(Component *subComponent) override{
        auto ret = std::find(children.begin(), children.end(),
                        subComponent);
        if (ret == children.end())
            return;

        children.erase(ret);
    }

    virtual Component *getChild(std::vector<Component*>::size_type index) override{
        if (index > children.size())
            return nullptr;
        return children[index];
    }
};

int main(void)
{
    Component *component = new Composite;
    component->add(new Composite);
    component->add(new Composite);
    component->add(new Leaf);
    component->add(new Leaf);
    component->getChild(1)->add(new Leaf);
    component->getChild(1)->add(new Leaf);
    component->getChild(1)->add(new Leaf);

    component->operation();
}

运行结果:

Here is composite. childen :
Here is composite. childen :
Here is composite. childen :
Here is leaf
Here is leaf
Here is leaf
Here is leaf
Here is leaf
### C++组合模式的详细介绍 #### 定义与概念 组合模式是一种结构型设计模式,允许将对象组合成树形结构来表示“部分-整体”的层次结构。该模式让客户端能以一致的方式处理单个对象和对象组合[^3]。 #### 特点 - **一致性**:无论是一个单独的对象还是多个对象组成的集合,在接口上都是一致的。 - **透明性**:对于客户来说,无论是访问单一对象还是复合对象都是相同的。 - **灵活性**:易于扩展新的组件类型而无需修改现有代码。 #### 缺点 尽管提供了上述优点,但也存在一些缺点: - 可能使设计更加复杂; - 减少了一定程度上的类型安全; --- ### 实现方式 为了实现这一模式,通常会定义一个抽象基类 `Component` 来声明公共的操作方法,并由具体部件(叶子节点)和容器(分支节点)共同继承此基类。下面展示了一个简单的例子: ```cpp // geom_example.h 文件中的 Component 类定义 class FileSystem { public: virtual ~FileSystem() {} // 显示名称的方法 virtual void ShowName(int level) = 0; // 计算文件数目的虚函数,默认返回0 virtual int countNumOfFiles() const { return 0; } }; // File 类作为叶子结点的具体实现 class File : public FileSystem { private: std::string m_sname; public: explicit File(const std::string& name): m_sname(name) {} void ShowName(int level) override { for (int i = 0; i < level; ++i) { std::cout << " "; } std::cout << "- " << m_sname << "\n"; } int countNumOfFiles() const override { return 1; // 单一文件的数量总是1 } }; ``` 接着是 Composite 部分,即可以容纳其他 Components 的 Container 或者说是 Folder: ```cpp #include <vector> using namespace std; // Directory 类作为一个包含子项的容器 class Directory : public FileSystem { private: vector<FileSystem*> children; string dir_name; public: Directory(const string& name) : dir_name(name) {} void add(FileSystem* component) { children.push_back(component); } void remove(FileSystem* component) { auto it = find(children.begin(), children.end(), component); if(it != children.end()) { delete *it; children.erase(it); } } void ShowName(int level) override { for (int i = 0; i < level; ++i) { cout << " "; } cout << "+ " << dir_name << "\n"; for(auto child : children){ child->ShowName(level + 2); } } int countNumOfFiles() const override { int total_files = 0; for(auto item : children){ total_files += item->countNumOfFiles(); } return total_files; } }; ``` --- ### 使用场景 当面对具有明显层次关系的数据集时,比如文件系统、图形界面控件管理或是企业内部组织架构等问题域内,都可以考虑采用组合模式来进行建模[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值