装饰模式C++实现

装饰模式(Decorator),动态的给一个对象添加一些额外的职责,先看图

装饰模式

Component是定义一个对象接口,可以给这些对象动态的添加职责。

ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。

Decorator装饰类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的

至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能

 

下面是具体的实现:

[cpp]  view plain copy
  1. //component.h  
  2.   
  3. #ifndef AFX_CLASS_COMPONENT  
  4. #define AFX_CLASS_COMPONENT  
  5. class Component  
  6. {  
  7. public:  
  8.     virtual void Operation()=0;  
  9. };  
  10. #endif  
  11.   
  12. #ifndef AFX_CLASS_CONCRETECOMPONENT  
  13. #define AFX_CLASS_CONCRETECOMPONENT  
  14. class ConcreteComponent:public Component  
  15. {  
  16. public:  
  17.     virtual void Operation()  
  18.     {  
  19.         cout<<"具体对象的操作"<<endl;  
  20.     }  
  21. };  
  22. #endif  
  23.   
  24. #ifndef AFX_CLASS_DECORATOR  
  25. #define AFX_CLASS_DECORATOR  
  26. class Decorator:public Component  
  27. {  
  28. public:  
  29.     void SetComponent(Component *component)  
  30.     {  
  31.         this->component = component;  
  32.     }  
  33.   
  34.     virtual void Operation()  
  35.     {  
  36.         if( component != NULL )  
  37.         {  
  38.             component->Operation();  
  39.         }  
  40.     }  
  41.   
  42. protected:  
  43.     Component *component;  
  44. };  
  45. #endif  
  46.   
  47. #ifndef AFX_CLASS_CONCRETEDECORATOR  
  48. #define AFX_CLASS_CONCRETEDECORATOR  
  49. class ConcreteDecoratorA:public Decorator  
  50. {  
  51. public:  
  52.     virtual void Operation()  
  53.     {  
  54.         Decorator::Operation();  
  55.         addedState = "New state" ;  
  56.         cout<<"具体装饰对象A的操作"<<endl;  
  57.     }  
  58. private:  
  59.     string addedState;  
  60. };  
  61.   
  62. class ConcreteDecoratorB:public Decorator  
  63. {  
  64. public:  
  65.     virtual void Operation()  
  66.     {  
  67.         Decorator::Operation();  
  68.         AddedBehavior();  
  69.         cout<<"具体装饰对象B的操作"<<endl;  
  70.     }  
  71. private:  
  72.     void AddedBehavior()  
  73.     {  
  74.     }  
  75. };  
  76. #endif  

 

 

接着是调用:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <conio.h>  
  4. using namespace std;  
  5. #include "component.h"  
  6.   
  7. int main(int argc,char *argv[])  
  8. {  
  9.     ConcreteComponent *c = new ConcreteComponent();  
  10.     ConcreteDecoratorA *d1 = new ConcreteDecoratorA();  
  11.     ConcreteDecoratorB *d2 = new ConcreteDecoratorB();  
  12.   
  13.     d1->SetComponent( c );  
  14.     d2->SetComponent( d1 );  
  15.     d2->Operation();  
  16.     getch();  
  17.     return 1;  
  18.   
  19. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值