设计模式之策略模式

来源:互联网 发布:被两个男人同时 知乎 编辑:程序博客网 时间:2024/04/29 07:24
  1. #include <iostream>
  2. using namespace std;
  3. // 飞行行为接口类
  4. class FlyStrategy
  5. {
  6. public:
  7.     virtual void Fly() = 0;
  8. protected:
  9.     FlyStrategy(){}
  10. };
  11. // 高空飞行行为具现类
  12. class HeightFly:public FlyStrategy
  13. {
  14. public:
  15.     static HeightFly &GetSingleton()
  16.     {
  17.         static HeightFly s_HeightFly;
  18.         return s_HeightFly;
  19.     }
  20.     virtual void Fly()
  21.     {
  22.         cout << "It is a heightfly behavior." << endl;
  23.     }
  24. private:
  25.     HeightFly(){}
  26. };
  27. // 低空飞行行为具现类
  28. class LowFly:public FlyStrategy
  29. {
  30. public:
  31.     static LowFly &GetSingleton()
  32.     {
  33.         static LowFly s_LowFly;
  34.         return s_LowFly;
  35.     }
  36.     virtual void Fly()
  37.     {
  38.         cout << "It is a lowfly behavior." << endl;
  39.     }
  40. private:
  41.     LowFly(){}
  42. };
  43. // 叫声行为接口类
  44. class SoundStrategy
  45. {
  46. public:
  47.     virtual void MakeSound() = 0;
  48. protected:
  49.     SoundStrategy(){}
  50. };
  51. // 大声叫行为具现类
  52. class LoudlySound:public SoundStrategy
  53. {
  54. public:
  55.     static LoudlySound &GetSingleton()
  56.     {
  57.         static LoudlySound s_LoudlySound;
  58.         return s_LoudlySound;
  59.     }
  60.     virtual void MakeSound()
  61.     {
  62.         cout << "It is a loudlysound behavior." << endl;
  63.     }
  64. private:
  65.     LoudlySound(){}
  66. };
  67. // 小声叫行为具现类
  68. class SmallSound:public SoundStrategy
  69. {
  70. public:
  71.     static SmallSound &GetSingleton()
  72.     {
  73.         static SmallSound s_SmallSound;
  74.         return s_SmallSound;
  75.     }
  76.     virtual void MakeSound()
  77.     {
  78.         cout << "It is a smallsound behavior." << endl;
  79.     }
  80. private:
  81.     SmallSound(){}
  82. };
  83. // 鸭子具现类
  84. class Duck
  85. {
  86. private:
  87.     // 行为接口
  88.     FlyStrategy *m_pFlyStrategy;
  89.     SoundStrategy *m_pSoundStrategy;
  90. public:
  91.     // 带参数构造函数
  92.     Duck(FlyStrategy *p_pFlyStrategy, SoundStrategy *p_pSoundStrategy):
  93.       m_pFlyStrategy(p_pFlyStrategy),m_pSoundStrategy(p_pSoundStrategy){}
  94.     // 飞行
  95.     void Fly()
  96.     {
  97.         m_pFlyStrategy->Fly();
  98.     }
  99.     // 发出叫声
  100.     void Sound()
  101.     {
  102.         m_pSoundStrategy->MakeSound();
  103.     }
  104.     // 动态设置行为接口
  105.     void SetFlyStrategy(FlyStrategy *p_pFlyStrategy)
  106.     {
  107.         m_pFlyStrategy = p_pFlyStrategy;
  108.     }
  109.     void SetSoundStrategy(SoundStrategy *p_pSoundStrategy)
  110.     {
  111.         m_pSoundStrategy = p_pSoundStrategy;
  112.     }
  113.     // 显示虚函数
  114.     virtual void Display() const
  115.     {
  116.         cout << "This is a ordinary duck." << endl;
  117.     }
  118. };
  119. int main()
  120. {
  121.     // 创建鸭子对象
  122.     Duck duck(&HeightFly::GetSingleton(), &LoudlySound::GetSingleton());
  123.     // 作出行为
  124.     duck.Fly();
  125.     duck.Sound();
  126.     // 动态切换鸭子行为
  127.     duck.SetFlyStrategy(&LowFly::GetSingleton());
  128.     duck.SetSoundStrategy(&SmallSound::GetSingleton());
  129.     // 作出新一轮的行为
  130.     duck.Fly();
  131.     duck.Sound();
  132.     system("pause");
  133.     return 0;
  134. }
  135. 策略模式理解:策略模式的核心理念是将类型中变换的部分(行为接口)分离到外部,并以特定的一组类对其进行实现,
  136.            而在原类型内部只保留各行为抽象基类接口的指针,通过委托的方式实现用户的请求并提供Setter函数方
  137.            便用户动态地改变行为的类型.
  138. 继承的缺点:白箱复用的特性导致子类默认继承一份缺省实现,将易变化的实现代码整合进类型定义将导致日后增加新
  139.       的实现或进行实现的修改尤为困难.每次当需要新的需求时都需要覆盖以前的实现,容易导致类泛滥,不能达到完
  140.       全的代码复用.
  141. 组合的优点:黑箱复用的方式不会暴露实现的细节,分离封装变化的部分增加可维护性和易扩展性,并带来完全的代码复用.
  142.       基于接口的编程使得可以动态的改变行为类型.