装饰器模式
使用场景:动态地给一个对象添加一些额外的职责。
例子:比如手机,有诺基亚手机、苹果手机,允许你为这些手机添加特性,比如增加挂件、屏幕贴膜等。这些挂件和贴膜就是装饰器。
具体实现:
class Phone { public: Phone() {} virtual ~Phone() {} virtual void ShowDecorate() {} }; class iPhone : public Phone { private: string m_name; //手机名称 public: iPhone(string name): m_name(name){} ~iPhone() {} void ShowDecorate() { cout<<m_name<<"的装饰"<<endl;} }; //具体的手机类 class NokiaPhone : public Phone { private: string m_name; public: NokiaPhone(string name): m_name(name){} ~NokiaPhone() {} void ShowDecorate() { cout<<m_name<<"的装饰"<<endl;}
装饰器类:
class DecoratorPhone : public Phone { private: Phone *m_phone; //要装饰的手机 public: DecoratorPhone(Phone *phone): m_phone(phone) {} virtual void ShowDecorate() { m_phone->ShowDecorate(); } }; //具体的装饰类 class DecoratorPhoneA : public DecoratorPhone { public: DecoratorPhoneA(Phone *phone) : DecoratorPhone(phone) {} void ShowDecorate() { DecoratorPhone::ShowDecorate(); AddDecorate(); } private: void AddDecorate() { cout<<"增加挂件"<<endl; } //增加的装饰 }; //具体的装饰类 class DecoratorPhoneB : public DecoratorPhone { public: DecoratorPhoneB(Phone *phone) : DecoratorPhone(phone) {} void ShowDecorate() { DecoratorPhone::ShowDecorate(); AddDecorate(); } private: void AddDecorate() { cout<<"屏幕贴膜"<<endl; } //增加的装饰 };
使用:
int main() { Phone *iphone = new NokiaPhone("6300"); Phone *dpa = new DecoratorPhoneA(iphone); //装饰,增加挂件 Phone *dpb = new DecoratorPhoneB(dpa); //装饰,屏幕贴膜 dpb->ShowDecorate(); delete dpa; delete dpb; delete iphone; return 0; }
参考:https://blog.csdn.net/wuzhekai1985/article/details/6667564