Composite模式 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    在开发时,如果遇到使用递归构建树状的组合结构,那么可以考虑使用Composite模式,

Composite模式

。Composite模式将对象组合成树形结构,来表示部分、整体的层次结构。

    其类结构如图所示:

   

    在Component中声明了所有管理子类对象的方法,因此实现Component接口的子类都有了Add、Remove等功能,这样叶子节点和枝节点对于外界没有区别;但是因为Leaf类本身不具备Add、Remove等功能,实现也没有意义。<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPtTayrXP1sqxo6y53MDt0rbX073atePQ6NKqyN3G96Os1eLA78q508PBy3ZlY3RvcqGjPC9wPg0KPHA+Ly9Db21wb25lbnQuaDwvcD4NCjxwcmUgY2xhc3M9"brush:java;">//Component.h#ifndef _COMPONENT_H_#define _COMPONENT_H_class Component{public: Component(); virtual ~Component(); virtual void Operation() = 0; virtual void Add(const Component&); virtual void Remove(const Component&); virtual Component* GetChild(int);};#endif

    //Component.cpp

<code class="hljs" vala="">//Component.cpp#includeComponent.hComponent::Component(){}Component::~Component(){}void Component::Add(const Component& com){}void Component::Remove(const Component& com){}Component* Component::GetChild(int index){    return 0;}</code>

    //Composite.h

<code class="hljs" cpp="">//Composite.h#ifndef _COMPOSITE_H_#define _COMPOSITE_H_#includeComponent.h#include<vector>class Composite :public Component{public:    Composite();    ~Composite();    void Operation();    void Add(Component* com);    void Remove(Component* com);    Component* GetChild(int index);private:    std::vector<component*>comVec;};#endif</component*></vector></code>

    //Composite.cpp

<code class="hljs" cpp="">//Composite.cpp#includeComponent.h#includeComposite.hComposite::Composite(){}Composite::~Composite(){}void Composite::Operation(){    std::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){    std::vector<component*>::iterator comIter = comVec.begin();    for (; comIter != comVec.end(); comIter++)    {        if (*comIter == com)        {            comVec.erase(comIter);            return;        }    }}Component* Composite::GetChild(int index){    return comVec[index];}</component*></component*></code>

    //Leaf.h

<code class="hljs" cs="">//Leaf.h#ifndef _LEAF_H_#define _LEAF_H_#includeComponent.hclass Leaf :public Component{public:    Leaf();    ~Leaf();    void Operation();};#endif</code>

    //Leaf.cpp

<code class="hljs" cpp="">//Leaf.cpp#includeLeaf.h#include<iostream>Leaf::Leaf(){}Leaf::~Leaf(){}void Leaf::Operation(){    std::cout << Leaf operation... << std::endl;}</iostream></code>

    //main.cpp

<code class="hljs" lasso="">//main.cpp#includeComponent.h#includeComposite.h#includeLeaf.hint main(){    Leaf* l = new Leaf();    l->Operation();    Composite *com = new Composite();    com->Add(l);    com->Operation();    Component* ll = com->GetChild(0);    ll->Operation();    return 0;}</code>

免费领课

    在开发时,如果遇到使用递归构建树状的组合结构,那么可以考虑使用Composite模式,

Composite模式

。Composite模式将对象组合成树形结构,来表示部分、整体的层次结构。

    其类结构如图所示:

   

    在Component中声明了所有管理子类对象的方法,因此实现Component接口的子类都有了Add、Remove等功能,这样叶子节点和枝节点对于外界没有区别;但是因为Leaf类本身不具备Add、Remove等功能,实现也没有意义。<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPtTayrXP1sqxo6y53MDt0rbX073atePQ6NKqyN3G96Os1eLA78q508PBy3ZlY3RvcqGjPC9wPg0KPHA+Ly9Db21wb25lbnQuaDwvcD4NCjxwcmUgY2xhc3M9"brush:java;">//Component.h#ifndef _COMPONENT_H_#define _COMPONENT_H_class Component{public: Component(); virtual ~Component(); virtual void Operation() = 0; virtual void Add(const Component&); virtual void Remove(const Component&); virtual Component* GetChild(int);};#endif

    //Component.cpp

<code class="hljs" vala="">//Component.cpp#includeComponent.hComponent::Component(){}Component::~Component(){}void Component::Add(const Component& com){}void Component::Remove(const Component& com){}Component* Component::GetChild(int index){    return 0;}</code>

    //Composite.h

<code class="hljs" cpp="">//Composite.h#ifndef _COMPOSITE_H_#define _COMPOSITE_H_#includeComponent.h#include<vector>class Composite :public Component{public:    Composite();    ~Composite();    void Operation();    void Add(Component* com);    void Remove(Component* com);    Component* GetChild(int index);private:    std::vector<component*>comVec;};#endif</component*></vector></code>

    //Composite.cpp

<code class="hljs" cpp="">//Composite.cpp#includeComponent.h#includeComposite.hComposite::Composite(){}Composite::~Composite(){}void Composite::Operation(){    std::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){    std::vector<component*>::iterator comIter = comVec.begin();    for (; comIter != comVec.end(); comIter++)    {        if (*comIter == com)        {            comVec.erase(comIter);            return;        }    }}Component* Composite::GetChild(int index){    return comVec[index];}</component*></component*></code>

    //Leaf.h

<code class="hljs" cs="">//Leaf.h#ifndef _LEAF_H_#define _LEAF_H_#includeComponent.hclass Leaf :public Component{public:    Leaf();    ~Leaf();    void Operation();};#endif</code>

    //Leaf.cpp

<code class="hljs" cpp="">//Leaf.cpp#includeLeaf.h#include<iostream>Leaf::Leaf(){}Leaf::~Leaf(){}void Leaf::Operation(){    std::cout << Leaf operation... << std::endl;}</iostream></code>

    //main.cpp

<code class="hljs" lasso="">//main.cpp#includeComponent.h#includeComposite.h#includeLeaf.hint main(){    Leaf* l = new Leaf();    l->Operation();    Composite *com = new Composite();    com->Add(l);    com->Operation();    Component* ll = com->GetChild(0);    ll->Operation();    return 0;}</code>