C++设计模式——组合模式(高屋建瓴)

tech2022-07-15  167

转载https://blog.csdn.net/CoderAldrich/article/details/83115374 C++组合模式代码修改

#include <iostream> #include <string> #include <vector> using namespace std; class Component { public: Component(string name) : m_strCompname(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() { return m_strCompname; } virtual void Print() = 0; protected: string m_strCompname; }; class Leaf : public Component { public: Leaf(string name) : Component(name) {} void Operation() { cout<<"I am"<<" "<<m_strCompname<<endl<<"\n"; } void Add(Component *) {} void Remove(Component *) {} Component *GetChild(int index) { return NULL; } void Print(){} }; class Composite : public Component { public: Composite(string name) : Component(name) {} ~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 Operation() { cout<<"I am"<<" "<<m_strCompname<<"\n"; } void Add(Component *pComponent) { m_vecComp.push_back(pComponent); } void 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 *GetChild(int index) { if (index > m_vecComp.size()) { return NULL; } return m_vecComp[index - 1]; } void Print() { for (vector<Component *>::iterator it = m_vecComp.begin(); it !=m_vecComp.end(); ++it) { cout<<(*it)->GetName()<<endl; } } private: vector<Component *> m_vecComp; }; int main() { 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->Operation(); 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->Operation(); pSubNodeSh->Add(pSubNodeShHr); pSubNodeSh->Add(pSubNodeShCg); pSubNodeSh->Add(pSubNodeShXs); pSubNodeSh->Add(pSubNodeShZb); pSubNodeSh->Print(); // 公司不景气,需要关闭上海质量监督部门 pSubNodeSh->Remove(pSubNodeShZb); if (pNode != NULL) { delete pNode; pNode = NULL; } return 0; } 输出: I am Beijing Head Office Beijing Human Resources Department Shanghai Branch Chengdu Branch Baotou Branch I am Shanghai Branch Shanghai Human Resources Department Shanghai Purchasing Department Shanghai Sales department Shanghai Quality supervision Department ----deleteBeijing Human Resources Department---- ----deleteShanghai Branch---- ----deleteShanghai Human Resources Department---- ----deleteShanghai Purchasing Department---- ----deleteShanghai Sales department---- ----deleteChengdu Branch---- ----deleteBaotou Branch----
最新回复(0)