设计模式-组合模式 C++实现

来源:互联网 发布:切片图片上传到淘宝 编辑:程序博客网 时间:2024/06/05 09:50

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

1.模式定义

组合模式(Composite Pattern)将小对象组合成树形结构,使用户操作组合对象如同操作一个单个对象。组合模式定义了“部分-整体”的层次结构,基本对象可以被组合成更大的对象,而且这种操作是可重复的,不断重复下去就可以得到一个非常大的组合对象,但这些组合对象与基本对象拥有相同的接口,因而组合是透明的,用法完全一致。

2.模式结构

组合模式包含如下角色:

Component:

为组合中的对象声明接口;在适当的情况下,实现所有类共有接口的缺省行为;声明一个接口用于访问和管理Component的子组件。

Leaf:

在组合中表示叶节点对象,叶节点没有子节点;在组合中定义叶节点的行为。

Composite:

定义有子部件的那些部件的行为;存储子部件。

这里写图片描述

3.代码分析

Conponent.h

#include <iostream>using namespace std;class Component{public:    Component(string compname) : m_strCompname(compname) {};    virtual ~Component() {};    virtual void Operation() = 0;    virtual void Add(Component *) = 0;    virtual void Remove(Component *) = 0;    virtual Component *GetChild(int) = 0;    virtual string GetName()    {        return m_strCompname;    }    virtual void Print() = 0;private:    string m_strCompname;};

Leaf.h

#include <iostream>#include "Component.h"using namespace std;class Leaf : public Component{public:    Leaf(string leafname) : Component(leafname) {};    virtual ~Leaf() {};    void Operation();    void Add(Component *pComponent);    void Remove(Component *pComponent);    Component *GetChild(int index);    void Print();};

Leaf.cpp

#include <string>#include "Leaf.h"void Leaf::Operation(){    cout << "Leaf " << GetName() << " Operation." << endl;}void Leaf::Add(Component *pComponent){}void Leaf::Remove(Component *pComponent){}Component * Leaf::GetChild(int index){    return NULL;}void Leaf::Print(){}

Composite.h

#include <iostream>#include <vector>#include "Component.h"using namespace std;class Composite : public Component{public:    Composite(string compname) : Component(compname) {};    virtual ~Composite();    void Operation();    void Add(Component *pComponent);    void Remove(Component *pComponent);    Component *GetChild(int index);    void Print();private:    vector<Component *> m_vecComp;};

Composite.cpp

#include <string>#include "Leaf.h"#include "Composite.h"Composite::~Composite(){    vector<Component *>::iterator it = m_vecComp.begin();    while (it != m_vecComp.end())    {        if (*it != NULL)        {            cout << "Composite " << GetName() << "delete " << ":" << (*it)->GetName() << endl;            delete *it;            *it = NULL;        }        m_vecComp.erase(it);        it = m_vecComp.begin();    }}void Composite::Operation(){    cout << "Composite " << GetName() << " Operation." << endl;}void Composite::Add(Component *pComponent){    m_vecComp.push_back(pComponent);}void Composite::Remove(Component *pComponent){    for (vector<Component *>::iterator it = m_vecComp.begin(); it != m_vecComp.end(); ++it)    {        if ((*it)->GetName() == pComponent->GetName())        {            if (*it != NULL)            {                delete *it;                *it = NULL;            }            m_vecComp.erase(it);            break;        }    }}Component * Composite::GetChild(int index){    if (index > m_vecComp.size())    {        return NULL;    }    return m_vecComp[index - 1];}void Composite::Print(){    for (vector<Component *>::iterator it = m_vecComp.begin(); it != m_vecComp.end(); ++it)    {        cout << "Composite " << GetName() << " contains : " << (*it)->GetName() << endl;    }}

测试代码:

int main(int argc, char *argv[]){    Component *pCompositeA = new Composite("CompositeA");    Component *pCompositeB = new Composite("CompositeB");    Component *pLeafA = new Composite("LeafA");    Component *pLeafB = new Composite("LeafB");    pCompositeA->Add(pLeafA);    pCompositeB->Add(pLeafB);    pCompositeA->Add(pCompositeB);    pCompositeB->Print();    pCompositeB->Operation();    cout << "--------------------------------------------------" << endl;    pCompositeA->Print();    cout << "--------------------------------------------------" << endl;    pCompositeA->Remove(pLeafA);    pCompositeA->Print();}

这里写图片描述

4.模式优点

使客户端调用简单,它可以一致使用组合结构或是其中单个对象,简化了客户端代码。

容易在组合体内增加对象部件。客户端不必因加入了新的部件而更改代码。有利于功能的扩展。

5.模式缺点

需要抉择使用透明方式还是安全方式。

透明方式违背了面向对象的单一职责原则;安全方式增加了客户需要端判定的负担。

6.总结

组合模式让我们能用树形方式创建对象的结构,树里面包含了Composite以及Leaf的对象。使用组合结构,我们能把相同的操作应用在Composite和Leaf上,即大多数情况下,我们可以忽略Composite和Leaf之间的差别,以相同的方式使用它们。为了保持透明性,Leaf和Composite都要继承或实现Component。

让管理子构件的方法如add()、remove()、getChild()等出现在Leaf中,是因为我们可以将Leaf看作是没有子构件的节点。

《设计模式》一书认为:在组合模式中,相对于安全性,我们比较强调透明性。对于透明式的组合模式中的Leaf内不需要的方法可以使用空处理或者异常报告的方式来解决。

Component中可以有一个指向父亲的指针,以便在游走时更容易。比如删除树形结构中的某个子树或叶节点(即Composite或Leaf)时会更加方便。

原创粉丝点击