首页 > 代码库 > Decorator
Decorator
#include <iostream>using namespace std;#define DESTROY_POINTER(ptr) if (ptr) { delete ptr; ptr = NULL; } class Product{public: virtual void Action()=0;};class ConcreteProductA : public Product{public: ConcreteProductA() {} ~ConcreteProductA() {} void Action() { cout<<"ConcreteProductA::Action"<<endl; }};class ConcreteProductB : public Product{public: ConcreteProductB() {} ~ConcreteProductB() {} void Action() { cout<<"ConcreteProductB::Action"<<endl; }};class Decorator : public Product{public: virtual void Action()=0;};class DecoratorProductA : public Decorator{public: DecoratorProductA() { m_pProduct = new ConcreteProductA; } ~DecoratorProductA() { DESTROY_POINTER(m_pProduct); } void AddModification() { cout<<"DecoratorProductA::AddModification->"; } void Action() { AddModification(); m_pProduct->Action(); } private: Product* m_pProduct;};class DecoratorProductB : public Decorator{public: DecoratorProductB() { m_pProduct = new ConcreteProductB; } ~DecoratorProductB() { DESTROY_POINTER(m_pProduct); } void AddModification() { cout<<"DecoratorProductB::AddModification->"; } void Action() { AddModification(); m_pProduct->Action(); }private: Product* m_pProduct;};int main(int argc, char *argv[]){ Product* pProduct; pProduct = new ConcreteProductA; pProduct->Action(); DESTROY_POINTER(pProduct); pProduct = new ConcreteProductB; pProduct->Action(); DESTROY_POINTER(pProduct); Decorator* pDecorator = new DecoratorProductA; pDecorator->Action(); DESTROY_POINTER(pDecorator); pDecorator = new DecoratorProductB; pDecorator->Action(); DESTROY_POINTER(pDecorator); return 0;}
Decorator
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。