Chinaunix首页 | 论坛 | 博客
  • 博客访问: 169427
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 22:37:21

策略模式 定义了算法族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。




  1. #include <iostream>
  2. #include <string>

  3. using namespace std;


  4. struct FlyBehavior {
  5. public:
  6.     virtual fly() = 0;
  7. };

  8. struct QuackBehavior {
  9. public:
  10.     virtual quack() = 0;
  11. };


  12. struct Duck {
  13. public:
  14.     swim() {}
  15.     virtual display() = 0;
  16.     preformQuack() {
  17.         flyBehavior->fly();
  18.     }
  19.     preformFly() {
  20.         quackBehavior->quack();
  21.     }
  22.     setFlyBehavior(FlyBehavior *mflyBehavior) {
  23.         flyBehavior = mflyBehavior;
  24.     }
  25.     setQuackBehavior(QuackBehavior *mquackBehavior) {
  26.         quackBehavior = mquackBehavior;
  27.     }
  28. protected:
  29.     FlyBehavior *flyBehavior;
  30.     QuackBehavior *quackBehavior;
  31. };


  32. struct FlyWithWings : public FlyBehavior {
  33.     virtual fly() {
  34.         cout << "\tFlyWithWings" << endl;
  35.     }
  36. };

  37. struct FlyNoWay : public FlyBehavior {
  38.     virtual fly() {
  39.         cout << "\tFlyNoWay" << endl;
  40.     }
  41. };

  42. struct Quack : public QuackBehavior {
  43.     virtual quack() {
  44.         cout << "\tQuack" << endl;
  45.     }
  46. };

  47. struct Squeak : public QuackBehavior {
  48.     virtual quack() {
  49.         cout << "\tSqueak" << endl;
  50.     }
  51. };

  52. struct MuteQuack : public QuackBehavior {
  53.     virtual quack() {
  54.         cout << "\tMuteQuack" << endl;
  55.     }
  56. };


  57. struct MallardDuck : public Duck {
  58.     MallardDuck() {
  59.         flyBehavior = new FlyNoWay();
  60.         quackBehavior = new Quack();
  61.     }
  62.     virtual display() {
  63.         cout << "MallardDuck:" << endl;
  64.     }
  65. };

  66. struct ReadheadDuck : public Duck {
  67.     ReadheadDuck() {
  68.         flyBehavior = new FlyWithWings();
  69.         quackBehavior = new MuteQuack();
  70.     }
  71.     virtual display() {
  72.         cout << "ReadheadDuck:" << endl;
  73.     }
  74. };

  75. struct RubberDuck : public Duck {
  76.     RubberDuck() {
  77.         flyBehavior = new FlyWithWings();
  78.         quackBehavior = new Quack();
  79.     }
  80.     virtual display() {
  81.         cout << "RubberDuck:" << endl;
  82.     }
  83. };

  84. struct DecoyDuck : public Duck {
  85.     DecoyDuck() {
  86.         flyBehavior = new FlyNoWay();
  87.         quackBehavior = new Squeak();
  88.     }
  89.     virtual display() {
  90.         cout << "DecoyDuck:" << endl;
  91.     }
  92. };

  93. int main(int argc, char **argv)
  94. {
  95.     MallardDuck *mallardDuck = new MallardDuck();
  96.     ReadheadDuck *readheadDuck = new ReadheadDuck();
  97.     RubberDuck *rubberDuck = new RubberDuck();
  98.     DecoyDuck *decoyDuck = new DecoyDuck();

  99.     mallardDuck->display();
  100.     mallardDuck->preformFly();
  101.     mallardDuck->preformQuack();
  102.     readheadDuck->display();
  103.     readheadDuck->preformFly();
  104.     readheadDuck->preformQuack();
  105.     rubberDuck->display();
  106.     rubberDuck->preformFly();
  107.     rubberDuck->preformQuack();
  108.     decoyDuck->display();
  109.     decoyDuck->preformFly();
  110.     decoyDuck->preformQuack();

  111.     delete mallardDuck;
  112.     delete readheadDuck;
  113.     delete rubberDuck;
  114.     delete decoyDuck;
  115. }
阅读(646) | 评论(0) | 转发(0) |
0

上一篇:设计模式

下一篇:观察者模式

给主人留下些什么吧!~~