头文件
//CompositePattern.h #ifndef COMPOSITE_PATTERN_H #define COMPOSITE_PATTERN_H #include <Windows.h> #include <iostream> #include <string> #include <vector> using namespace std; namespace CompositePattern { // ////////////////////////////////////////////////////////////////////////// class Component { public: Component(string name); virtual ~Component(); virtual void Operation() = 0; virtual void Add(Component *) = 0; virtual void Remove(Component *) = 0; virtual Component *GetChild(int) = 0; virtual string GetName(); virtual void Print() = 0; protected: string m_strCompname; }; class Leaf : public Component { public: Leaf(string name); virtual void Operation(); virtual void Add(Component *pComponent); virtual void Remove(Component *pComponent); virtual Component *GetChild(int index); virtual void Print(); }; class Composite : public Component { public: Composite(string name); virtual ~Composite(); virtual void Operation(); virtual void Add(Component *pComponent); virtual void Remove(Component *pComponent); virtual Component *GetChild(int index); virtual void Print(); private: vector<Component*> m_vecComp; }; ////////////////////////////////////////////////////////////////////////// void CompositePattern_Test(); } #endif
实现
#include "CompositePattern.h" #include <iostream> using namespace std; namespace CompositePattern { ////////////////////////////////////////////////////////////////////////// Component::Component(string name) : m_strCompname(name) { } Component::~Component(){} ////////////////////////////////////////////////////////////////////////// string Component::GetName() { return m_strCompname; } Leaf::Leaf(string name) : Component(name) {} void Leaf::Operation() { cout<<"I'm "<<m_strCompname<<endl; } void Leaf::Add(Component *pComponent){} void Leaf::Remove(Component *pComponent){} Component* Leaf::GetChild(int index) { return NULL; } void Leaf::Print(){} ////////////////////////////////////////////////////////////////////////// Composite::Composite(string name) : Component(name) {} Composite::~Composite() { vector<Component *>::iterator it = m_vecComp.begin(); while (it != m_vecComp.end()) { if (*it != NULL) { cout<<"----delete "<<(*it)->GetName()<<"----"<<endl; delete *it; *it = NULL; } m_vecComp.erase(it); it = m_vecComp.begin(); } } void Composite::Operation() { cout<<"I'm "<<m_strCompname<<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<<(*it)->GetName()<<endl; } } ////////////////////////////////////////////////////////////////////////// void CompositePattern_Test() { Component *pNode = new Composite("Beijing Head Office"); Component *pNodeHr = new Leaf("Beijing Human Resources Department"); Component *pSubNodeSh = new Composite("Shanghai Branch"); Component *pSubNodeCd = new Composite("Chengdu Branch"); Component *pSubNodeBt = new Composite("Baotou Branch"); pNode->Add(pNodeHr); pNode->Add(pSubNodeSh); pNode->Add(pSubNodeCd); pNode->Add(pSubNodeBt); pNode->Print(); Component *pSubNodeShHr = new Leaf("Shanghai Human Resources Department"); Component *pSubNodeShCg = new Leaf("Shanghai Purchasing Department"); Component *pSubNodeShXs = new Leaf("Shanghai Sales department"); Component *pSubNodeShZb = new Leaf("Shanghai Quality supervision Department"); pSubNodeSh->Add(pSubNodeShHr); pSubNodeSh->Add(pSubNodeShCg); pSubNodeSh->Add(pSubNodeShXs); pSubNodeSh->Add(pSubNodeShZb); pNode->Print(); // 公司不景气,需要关闭上海质量监督部门 pSubNodeSh->Remove(pSubNodeShZb); if (pNode != NULL) { delete pNode; pNode = NULL; } } }
客户端
#include "CompositePattern.h" #include <iostream> using namespace std; using namespace CompositePattern; void main() { CompositePattern_Test(); }
运行结果