设计模式 之 《装饰模式》
#ifndef __DECORATOR__ #define __DECORATOR__ #include <string> #include <iostream> using namespace std; //人 class Person { public: Person(string strName) { m_strName = strName; } Person(){} virtual void show() { cout<<"装扮的是:"<<m_strName<<endl; } private: string m_strName; }; //装饰类 class Finery : public Person { protected: Person* m_component; public: void Decorator(Person* component) { m_component = component; } virtual void show() { m_component->show(); } }; class TShirts : public Finery { public: virtual void show() { cout<<"T Shirts"<<endl; m_component->show(); } }; class BigTrouser : public Finery { public: virtual void show() { cout<<"Big Trouser"<<endl; m_component->show(); } }; #endif //__DECORATOR__ int _tmain(int argc, _TCHAR* argv[]) { Person* person = new Person("张三"); BigTrouser* bt = new BigTrouser(); TShirts* ts = new TShirts(); bt->Decorator(person); ts->Decorator(bt); ts->show(); return 0; }
Dreams are one of those things that keep you going and happy!!!