装饰者模式及C++ 2

装饰者模式:动态将职责附加到对象上,若要扩展功能,装饰者提供了比继承更具弹性的代替方案。

UML图

设计原则:

1. 多用组合,少用继承。

利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而,如果能够利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。

2. 类应设计的对扩展开放,对修改关闭。

要点:

1. 装饰者和被装饰对象有相同的超类型。

2. 可以用一个或多个装饰者包装一个对象。

3. 装饰者可以在所委托被装饰者的行为之前或之后,加上自己的行为,以达到特定的目的。

4. 对象可以在任何时候被装饰,所以可以在运行时动态的,不限量的用你喜欢的装饰者来装饰对象。

5. 装饰模式中使用继承的关键是想达到装饰者和被装饰对象的类型匹配,而不是获得其行为。

6. 装饰者一般对组件的客户是透明的,除非客户程序依赖于组件的具体类型。在实际项目中可以根据需要为装饰者添加新的行为,做到“半透明”装饰者。

7. 适配器模式的用意是改变对象的接口而不一定改变对象的性能,而装饰模式的用意是保持接口并增加对象的职责。

源代码:

Decorator.h

  1. #include <string>  
  2. #include <iostream>  
  3. #include <memory>  
  4. using namespace std;  
  5.   
  6. //抽象类Tank  
  7. class Tank  
  8. {  
  9. public:  
  10.     virtual void shot()=0;  
  11.     virtual void run()=0;  
  12.   
  13. public:  
  14.     virtual ~Tank()  
  15.     {  
  16.         cout<<"in the destructor of Tank"<<endl;  
  17.     }     
  18. };  
  19. //具体类 T50  
  20. class T50:public Tank  
  21. {  
  22. public:  
  23.     void shot()  
  24.     {  
  25.         cout<<"Tank T50 shot()"<<endl;  
  26.     }  
  27.     void run()  
  28.     {  
  29.         cout<<"Tank T50 run()"<<endl;  
  30.     }  
  31. public:  
  32.     virtual ~T50()  
  33.     {  
  34.         cout<<"In the destructor of T50"<<endl;  
  35.     }  
  36. };  
  37. //具体类T75  
  38. class T75:public Tank  
  39. {  
  40. public:  
  41.     void shot()  
  42.     {  
  43.         cout<<"Tank T75 shot()"<<endl;  
  44.     }  
  45.     void run()  
  46.     {  
  47.         cout<<"Tank T75 run()"<<endl;  
  48.     }  
  49. public:  
  50.     virtual ~T75()  
  51.     {  
  52.         cout<<"In the destructor of T75"<<endl;  
  53.     }  
  54. };  
  55. //具体类 T90  
  56. class T90:public Tank  
  57. {  
  58. public:  
  59.     void shot()  
  60.     {  
  61.         cout<<"Tank T90 shot()"<<endl;  
  62.     }  
  63.     void run()  
  64.     {  
  65.         cout<<"Tank T90 run()"<<endl;  
  66.     }  
  67. public:  
  68.     virtual ~T90()  
  69.     {  
  70.         cout<<"In the destructor of T90"<<endl;  
  71.     }  
  72. };  
  73.   
  74. //抽象类,Decorator  
  75. class Decorator:public Tank  
  76. {  
  77. protected:  
  78.     auto_ptr<Tank> tank;  
  79. public:  
  80.     Decorator(auto_ptr<Tank> tank):tank(tank) {}  //具体的坦克的装饰类  
  81.     virtual ~Decorator()  
  82.     {  
  83.         cout<<"In the destructor of Decorator"<<endl;  
  84.     }  
  85. public:  
  86.     void shot()  
  87.     {  
  88.         tank->shot();  
  89.     }  
  90.     void run()  
  91.     {  
  92.         tank->run();  
  93.     }  
  94. };  
  95.   
  96. class InfraredDecorator: public Decorator  
  97. {  
  98. private:  
  99.     string infrared;//这就是所谓的addAtrribute  
  100. public:  
  101.     InfraredDecorator(auto_ptr<Tank> tank):Decorator(tank) {}  
  102.     virtual ~InfraredDecorator()  
  103.     {  
  104.         cout<<"in the destructor of InfraredDecorator"<<endl;  
  105.     }  
  106. public:  
  107.     void set_Infrared(const string &infrared)  
  108.     {  
  109.         this->infrared=infrared;  
  110.     }  
  111.     string get_infrared() const  
  112.     {  
  113.         return infrared;  
  114.     }  
  115.     void run()  
  116.     {  
  117.         tank->run();  
  118.         set_Infrared("+Infrared");  
  119.         cout<<get_infrared()<<endl;  
  120.     }  
  121.     void shot()  
  122.     {  
  123.         tank->shot();  
  124.     }  
  125. };  
  126.   
  127. class AmphibianDecorator:public Decorator  
  128. {  
  129. private:  
  130.     string amphibian;  
  131. public:  
  132.     AmphibianDecorator(auto_ptr<Tank> tank):Decorator(tank) {}  
  133.     ~AmphibianDecorator()  
  134.     {  
  135.         cout<<"in the destructor of AmphibianDecorator"<<endl;  
  136.     }  
  137. public:  
  138.     void set_amphibian(const string &hibian)  
  139.     {  
  140.         this->amphibian=amphibian;  
  141.     }  
  142.     string get_amphibian() const  
  143.     {  
  144.         return amphibian;  
  145.     }  
  146. public:  
  147.     void run()  
  148.     {  
  149.         tank->run();  
  150.         set_amphibian("+amphibian");  
  151.         cout<<get_amphibian()<<endl;  
  152.     }  
  153.     void shot()  
  154.     {  
  155.         tank->shot();  
  156.     }  
  157. };  
  158.   
  159. class GPSDecorator:public Decorator  
  160. {  
  161. private:  
  162.     string gps;  
  163. public:  
  164.     GPSDecorator(auto_ptr<Tank> tank):Decorator(tank) {}  
  165.     ~GPSDecorator()  
  166.     {  
  167.         cout<<"in the destructor of GPSDecorator"<<endl;  
  168.     }  
  169. public:  
  170.     void set_gps(const string &gps)  
  171.     {  
  172.         this->gps=gps;  
  173.     }  
  174.     string get_gps() const  
  175.     {  
  176.         return gps;  
  177.     }  
  178. public:  
  179.     void run()  
  180.     {  
  181.         tank->run();  
  182.         set_gps("+gps");  
  183.         cout<<get_gps()<<endl;  
  184.     }  
  185.     void shot()  
  186.     {  
  187.         tank->shot();  
  188.     }  
  189. };  


Decorator.cpp

  1. #include "Decorator.h"  
  2. int main(int argc, char **argv)  
  3. {  
  4.     //给T50增加红外功能  
  5.     auto_ptr<Tank> tank1(new T50);  
  6.     auto_ptr<Tank> pid1(new InfraredDecorator(tank1));  
  7.     pid1->shot();  
  8.     cout<<endl;  
  9.     pid1->run();  
  10.     cout<<endl;  
  11.     cout<<endl<<"---------------"<<endl;  
  12.     //给t75增加红外、两栖功能  
  13.     auto_ptr<Tank> tank2(new T75);  
  14.     auto_ptr<Tank> pid2(new InfraredDecorator(tank2));  
  15.     auto_ptr<Tank> pad2(new AmphibianDecorator(pid2));  
  16.     pad2->shot();  
  17.     cout<<endl;  
  18.     pad2->run();  
  19.     cout<<endl;  
  20.     cout<<endl<<"--------------"<<endl;  
  21.     //给T75增加红外,两栖,定位功能  
  22.     auto_ptr<Tank> tank3(new T75);  
  23.     auto_ptr<Tank> pid3(new InfraredDecorator(tank3));  
  24.     auto_ptr<Tank> pad3(new AmphibianDecorator(pid3));  
  25.     auto_ptr<Tank> pgd3(new GPSDecorator(pad3));  
  26.     pgd3->shot();  
  27.     cout<<endl;  
  28.     pgd3->run();  
  29.     cout<<endl;  
  30.   
  31.   
  32.   
  33.     return 0;  
  34. }  


例2:

Test_Decorator.cpp

  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4. class Person  
  5. {  
  6. private:  
  7.     string name;  
  8. public:  
  9.     Person()  
  10.     {  
  11.     }  
  12.     Person(string name)  
  13.     {  
  14.         this->name=name;  
  15.     }  
  16.     virtual void show()  
  17.     {  
  18.         cout<<" Decorator name "<<name<<endl;  
  19.     }  
  20. };  
  21.   
  22. class Decorator:public Person  
  23. {  
  24. protected:  
  25.     Person* component;  
  26. public:  
  27.     void Deco(Person *component)  
  28.     {  
  29.         this->component=component;  
  30.     }  
  31.     void show()  
  32.     {  
  33.         component->show();  
  34.     }  
  35. };  
  36.   
  37. class toufa:public Decorator  
  38. {  
  39. public:  
  40.     void show()  
  41.     {  
  42.         cout<<"头发"<<endl;  
  43.         component->show();  
  44.     }  
  45. };  
  46.   
  47. class shoot:public Decorator  
  48. {  
  49. public:  
  50.     void show()  
  51.     {  
  52.         cout<<"鞋子"<<endl;  
  53.         component->show();  
  54.     }  
  55. };  
  56.   
  57. int main()  
  58. {  
  59.     Person fuli("fuli");  
  60.   
  61.     shoot xie;  
  62.     toufa tou;  
  63.     xie.Deco(&fuli);  
  64.     tou.Deco(&xie);  
  65.     tou.show();  
  66.     return 0;  
  67. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值