C++设置模式之组合(Composite )模式

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。

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

主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

何时使用: 1、您想表示对象的部分-整体层次结构(树形结构)。 2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

如何解决:树枝和叶子实现统一接口,树枝内部组合该接口。

关键代码:树枝内部组合该接口,并且含有内部属性 List,里面放 Component。

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

优点: 1、高层模块调用简单。 2、节点自由增加。

缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

使用场景:部分、整体场景,如树形菜单,文件、文件夹的管理。

注意事项:定义时为具体类。

Composite 模式的典型结构图为:

完整代码示例

Component.h

#ifndef _COMPONENT_H_
#define _COMPONENT_H_

class Component
{
public:
    Component();
    virtual ~Component();

public:
    virtual void Operation() = 0;
    virtual void Add(const Component& );
    virtual void Remove(const Component& );
    virtual Component* GetChild(int );
protected:
private:
};
#endif //~_COMPONENT_H_

Component.cpp

#include "Component.h"
Component::Component()
{
}

Component::~Component()
{
}

void Component::Add(const Component& com)
{
}

Component* Component::GetChild(int index)
{
    return 0;
}

void Component::Remove(const Component& com)
{
}

Composite.h

#ifndef _COMPOSITE_H_
#define _COMPOSITE_H_

#include "Component.h"
#include <vector>

using namespace std;

class Composite:public Component
{
public:
    Composite();
    ~Composite();
public:
    void Operation();
    void Add(Component* com);
    void Remove(Component* com);
    Component* GetChild(int index);
protected:
private:
    vector<Component*> comVec;
};
#endif //~_COMPOSITE_H_

Composite.cpp

#include "Composite.h"
#include "Component.h"

#define NULL 0 //define NULL POINTOR

Composite::Composite()
{
    //vector<Component*>::iterator itend = comVec.begin();
}

Composite::~Composite()
{
}

void Composite::Operation()
{
    vector<Component*>::iterator comIter = comVec.begin();
    for (;comIter != comVec.end();comIter++)
    {
        (*comIter)->Operation();
    }
}

void Composite::Add(Component* com)
{
    comVec.push_back(com);
}

void Composite::Remove(Component* com)
{
    comVec.erase(&com);
}

Component* Composite::GetChild(int index)
{
    return comVec[index];
}

Leaf.h

#ifndef _LEAF_H_
#define _LEAF_H_

#include "Component.h"

class Leaf:public Component
{
public:
    Leaf();
    ~Leaf();
    void Operation();
protected:
private:
};
#endif //~_LEAF_H_

Leaf.cpp

#include "Leaf.h"
#include <iostream>

using namespace std;

Leaf::Leaf()
{
}

Leaf::~Leaf()
{
}

void Leaf::Operation()
{
    cout<<"Leaf operation....."<<endl;
}

main.cpp

#include "Component.h"
#include "Composite.h"
#include "Leaf.h"
#include <iostream>

using namespace std;

int main(int argc,char* argv[])
{
    Leaf* l = new Leaf();
    l->Operation();
    Composite* com = new Composite();
    com->Add(l);
    com->Operation();
    Component* ll = com->GetChild(0);
    ll->Operation();
    return 0;
}

Composite 模式在实现中有一个问题就是要提供对于子节点( Leaf)的管理策略,这里使用的是 STL 中的 vector,可以提供其他的实现方式,如数组、链表、 Hash 表等。

 


版权声明:本文为yangxueyangxue原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>