/********************************* *设计模式--装饰模式实现 *C++语言 *Author:WangYong *Blog:http://www.cnblogs.com/newwy ********************************/ #include <iostream> using namespace std; class Component { public: virtual ~Component(){} Component(){} virtual void Operation(){} }; class ConcreteComponent:public Component { public: ConcreteComponent(){} ~ConcreteComponent(){} void Operation(){cout<<"ConcreteComponent operation ..."<<endl;} }; class Decorator:public Component { public: Decorator(Component*com){this->_com = com;} virtual ~Decorator(){} void Operation(){} protected: Component * _com; }; class ConcreteDecrator:public Decorator { public: ConcreteDecrator(Component*com):Decorator(com){} ~ConcreteDecrator(){} void Operation(){_com->Operation();} void AddedBehavior(){cout<<"ConcreteDecrator::AddedBehavior..."<<endl;} }; int main() { Component*com = new ConcreteComponent(); Decorator *dec = new ConcreteDecrator(com); dec->Operation(); delete dec; return 0; }