C++ 策略模式

策略模式(Strategy):它定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法的变化不会影响到使用算法的客户。(原文:The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.)

UML图

优点:

  1、 简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。
  2、 避免程序中使用多重条件转移语句,使系统更灵活,并易于扩展。
        3、 遵守大部分GRASP原则和常用设计原则,高内聚、低偶合。

缺点:
  1、 因为每个具体策略类都会产生一个新类,所以会增加系统需要维护的类的数量。
         2、 在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给策略模式的Context对象

源代码:

Strategy.h

[cpp]  view plain  copy
 print ?
  1. #include <iostream>  
  2. #include <string>  
  3. #include <memory>  
  4. using namespace std;  
  5.   
  6. //strategy抽象类,用作接口  
  7. class Strategy  
  8. {  
  9. public:  
  10.     virtual string substitute(string str)=0;  
  11.     virtual ~Strategy()  
  12.     {  
  13.         cout<<" in the destructor of Strategy"<<endl;  
  14.     }  
  15. };  
  16.   
  17. class ChineseStrategy:public Strategy  
  18. {  
  19. public:  
  20.     string substitute(string str)  
  21.     {  
  22.         int index=str.find("520");  
  23.         string tempstr=str.replace(index,3,"我爱你");  
  24.         return tempstr;  
  25.     }  
  26.     ~ChineseStrategy()  
  27.     {  
  28.         cout<<"in the destructor of ChineseStrategy"<<endl;  
  29.     }  
  30. };  
  31.   
  32. class EnglishStrategy:public Strategy  
  33. {  
  34. public:  
  35.     string substitute(string str)  
  36.     {  
  37.         int index=str.find("520");  
  38.         string tempstr=str.replace(index,3,"i love ou");  
  39.         return tempstr;  
  40.     }  
  41.     ~EnglishStrategy()  
  42.     {  
  43.         cout<<" in the destructor of ChineseStrategy"<<endl;  
  44.     }  
  45. };  
  46.   
  47. //Context类  
[cpp]  view plain  copy
 print ?
  1. class Translator  
  2. {  
  3. private:  
  4.     auto_ptr<Strategy> strategy;  
[cpp]  view plain  copy
 print ?
  1.          //在客户代码中加入算法(stategy)类型的指针。  
  2. public:  
  3.     ~Translator()  
  4.     {  
  5.         cout<<" in the destructor of Translator"<<endl;  
  6.     }  
  7.     void set_strategy(auto_ptr<Strategy> strategy)  
  8.     {  
  9.         this->strategy=strategy;  
  10.     }  
  11.     string translate(string str)  
  12.     {  
  13.         if(0==strategy.get())  
  14.             return "";  
  15.         return strategy->substitute(str);  
  16.     }  
  17. };  


Strategy.cpp

[cpp]  view plain  copy
 print ?
  1. #include "Strategy.h"  
  2.   
  3. int main(int argc, char *argv)  
  4. {  
  5.     string str("321520");  
  6.     Translator *translator=new Translator;  
  7.     //未指定strategy的时候  
  8.     cout<<"No Strategy"<<endl;  
  9.     translator->translate(str);  
  10.     cout<<"---------------"<<endl;  
  11.       
  12.     //翻译成中文  
  13.     auto_ptr<Strategy> s1(new ChineseStrategy);  
  14.     translator->set_strategy(s1);  
  15.     cout<<"Chinese Strategy"<<endl;  
  16.     cout<<translator->translate(str)<<endl;  
  17.     cout<<"---------------"<<endl;  
  18.   
  19.     //翻译成英文  
  20.     auto_ptr<Strategy> s2(new EnglishStrategy);  
  21.     translator->set_strategy(s2);  
  22.     cout<<"English Strategy"<<endl;  
  23.     cout<<translator->translate(str)<<endl;  
  24.     cout<<"----------------"<<endl;  
  25.   
  26.     delete translator;  
  27.     return 0;  
  28.   
  29. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值