设计模式--组成模式实现C++
2010-10-18 23:41
429 查看
/********************************* *设计模式--组成模式实现 *C++语言 *Author:WangYong *Blog:http://www.cnblogs.com/newwy ********************************/ #include <iostream> #include <vector> using namespace std; class Component { public: Component(){} ~Component(){} virtual void Operation() = 0; virtual void Add(const Component &){} virtual void Remove(const Component&){} virtual Component *GetChild(int){return 0;} }; class Composite:public Component { public: Composite(){vector<Component*>::iterator itend = comVec.begin();} ~Composite(){} void Operation() { vector<Component*>::iterator comIter = comVec.begin(); for(;comIter != comVec.end(); comIter++) { (*comIter)->Operation(); } } void Add(Component *com){comVec.push_back(com);} void Remove(Component *com){comVec.erase(&com);} Component * GetChild(int index){return comVec[index];} private: vector<Component*> comVec; }; class Leaf:public Component { public: Leaf(){} ~Leaf(){} void Operation(){cout<<"Leaf operation.."<<endl;} }; int 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; }
相关文章推荐
- C++实现设计模式之工厂模式
- 设计模式C++实现(14)——职责链模式
- C++ 实现 设计模式 系列
- 设计模式C++实现(14)——职责链模式
- 设计模式C++实现--策略模式
- 设计模式C++实现--装饰模式
- 使用设计模式中的单例模式来实现C++的boost库
- 设计模式---单件(C++版) 比较通用的单件模式(STL实现)
- 我所理解的设计模式(C++实现)——备忘录模式(Memento Pattern)
- 设计模式C++实现(10)——桥接模式
- 设计模式C++实现(7)——外观模式、组合模式
- 设计模式---解释器模式(C++实现)
- 我所理解的设计模式(C++实现)——解释器模式(Interpreter Pattern)
- 设计模式学习(C++实现)6--桥接模式
- 设计模式C++实现(3)——建造者模式(Builder)
- 设计模式--备忘录模式C++实现
- 设计模式(C++实现)之Command
- 最常用的设计模式---观察者模式(C++实现)
- 《Head First 设计模式》例子的C++实现(1 策略模式)
- c++设计模式的实现