未名
5毛党党员
全部博文(17)
2011年(1)
2009年(16)
anank
7717060
flw
HonestQi
scutan
cugb_cat
浅尝寂寞
gong_yue
分类: C/C++
2009-02-24 16:42:43
#include <string> #include <iostream> using namespace std; class Component { public: virtual void Operation(){}; }; class ConcreateComponent :public Component { public: void Operation() { cout<<"具体对象的操作"<<endl; } }; class Decorator : public Component { protected: Component* m_component; public: Decorator() { m_component = NULL; } void SetCompenent(Component* component) { m_component = component; } virtual void Operation() { if (m_component != NULL) { m_component->Operation(); } } }; class ConcreateDecoratorA : public Decorator { private: //Decorator m_decorator; string addedState; public: //ConcreateDecoratorA(): void Operation() { Decorator::Operation(); addedState = "New state"; cout<<"具体装饰对象A的操作"<<endl; } }; class ConcreateDecoratorB : public Decorator { private: //Decorator m_decorator; //string addedState; public: //ConcreateDecoratorA(): void Operation() { Decorator::Operation(); cout<<"具体装饰对象B的操作"<<endl; } }; int main(void) { ConcreateComponent* c = new ConcreateComponent(); ConcreateDecoratorA *da = new ConcreateDecoratorA(); ConcreateDecoratorA *da2 = new ConcreateDecoratorA(); ConcreateDecoratorB *db = new ConcreateDecoratorB(); da->SetCompenent(c); db->SetCompenent(da); da2->SetCompenent(db); da2->Operation(); };
上一篇:模板方法模式(TemplateMethod)_c++
下一篇:策略模式(Strategy)+简单工厂_c++
登录 注册