Strategy Pattern

The strategy pattern is to take the parts that vary and encapsulation them, so that later you can alter or extend the parts that vary without affecting those that don't. 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. 

In the head first design pattern book, the example said that : "fly() and quack() are the parts of the Duck class that vary across ducks". "To separate these behaviours from the Duck class, we will pull both methods out of the Duck class and Create a new set of classes to represent each behaviour. " To make use of this pattern, it is better to first get familiar with polymorphism "dynamic binding

Sample C++ code:

#include "../header.h"
using namespace std;

class QuackBehavior {
public:
  virtual void quack() = 0;
};

class QuackWithSound : public QuackBehavior {
public:
  QuackWithSound() {}
  ~QuackWithSound() {}
  void quack() {
    cout << "quack with sound" << endl;
  }
};

class QuackNoSound : public QuackBehavior {
public:
  QuackNoSound() {}
  ~QuackNoSound() {}
  void quack() {
    cout << "quack no sound" << endl;
  }
};

class Duck{
private:
  QuackBehavior* Dquack;
public:
  Duck() {}
  virtual ~Duck() {}
  void swim() {
    cout << "a duck can swim" << endl;
  }
  void setQuackBehavior(QuackBehavior* qBehavior) {
    Dquack = qBehavior;
  }
  void performQuack() {
    Dquack->quack();
  }
};

class woodenDuck : public Duck{
public:
  woodenDuck() {}
  ~woodenDuck() {}
};

class redDuck : public Duck {
  public:
    redDuck() {}
    ~redDuck() {}
};

int main(void) {
  QuackBehavior* qBehavior = new QuackWithSound();
  Duck* duck = new redDuck();
  duck->setQuackBehavior(qBehavior);
  duck->performQuack();
}

Maybe better to use "protected" instead of initialize the variable.

#include "../header.h"
using namespace std;

class QuackBehavior {
public:
  virtual void quack() = 0;
};

class QuackWithSound : public QuackBehavior {
public:
  QuackWithSound() {}
  ~QuackWithSound() {}
  void quack() {
    cout << "quack with sound" << endl;
  }
};

class QuackNoSound : public QuackBehavior {
public:
  QuackNoSound() {}
  ~QuackNoSound() {}
  void quack() {
    cout << "quack no sound" << endl;
  }
};

class Duck{
protected:
  QuackBehavior* Dquack;
public:
  Duck() {}
  virtual ~Duck() {}
  void swim() {
    cout << "a duck can swim" << endl;
  }
  void performQuack() {
    Dquack->quack();
  }
};

class woodenDuck : public Duck{
public:
  woodenDuck() {
    Dquack = new QuackNoSound();
  }
  ~woodenDuck() {}
};

class redDuck : public Duck {
public:
    redDuck() {
      Dquack = new QuackWithSound();
    }
    ~redDuck() {}
};

int main(void) {
  Duck* duck = new woodenDuck();
  duck->performQuack();

  duck = new redDuck();
  duck->performQuack();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值