(一)Head first design patterns策略模式(c++)

引子

经典的设计模式有23种,分别是

创建型模式:工厂方式模式、抽象工厂模式、单例模式、建造者模式、原型模式。

结构型模式:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、蝇量模式。

行为型模式:策略模式、模板式模式、观察者模式、迭代器模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。

策略模式

子类继承父类可以获得父类的属性和方法。子类通过方法的重写去实现自身方法的独特性。但是当多个子类中的部分子类用的还是同一种方法,这就会造成大量重复的代码。而且有的泳姿并不一定需要breathe(开个玩笑哈哈),如果在父类中定义了breathe(),后面的子类就都会继承这个行为。用继承的方式肯定是不够灵活的。

采用继承方法描述对象

(一)Head first design patterns策略模式(c++)_第1张图片

示例代码:继承方式

#include

class Swimming
{
public:
    virtual void warmUp(){}
    virtual void coolDown(){}
    virtual void breathe(){}
    virtual void armMove(){}
    virtual void legMove(){}
};
class ButterflySwimming : public Swimming
{
public:
    void warmUp(){   std::cout << "warm up   in butterflyswimming type" << std::endl; }
    void coolDown(){ std::cout << "cool down in butterflyswimming type" << std::endl; }
    void breathe(){  std::cout << "breathe   in butterflyswimming type" << std::endl; }
    void armMove(){  std::cout << "arm move  in butterflyswimming type" << std::endl; }
    void legMove(){  std::cout << "leg move  in butterflyswimming type" << std::endl; }
};
class FreeStyleSwimming : public Swimming
{
public:
    void warmUp(){   std::cout << "warm up   in freestyleswimming type" << std::endl; }
    void coolDown(){ std::cout << "cool down in freestyleswimming type" << std::endl; }
    void breathe(){  std::cout << "breathe   in freestyleswimming type" << std::endl; }
    void armMove(){  std::cout << "arm move  in freestyleswimming type" << std::endl; }
    void legMove(){  std::cout << "leg move  in freestyleswimming type" << std::endl; }
};
class BreastStrokeSwimming : public Swimming
{
public:
    void warmUp(){   std::cout << "warm up   in breaststrokeswimming type" << std::endl; }
    void coolDown(){ std::cout << "cool down in breaststrokeswimming type" << std::endl; }
    void breathe(){  std::cout << "breathe   in breaststrokeswimming type" << std::endl; }
    void armMove(){  std::cout << "arm move  in breaststrokeswimming type" << std::endl; }
    void legMove(){  std::cout << "leg move  in breaststrokeswimming type" << std::endl; }
};
class BackStrokeSwimming : public Swimming
{
public:
    void warmUp(){   std::cout << "warm up   in backstrokeswimming type" << std::endl; }
    void coolDown(){ std::cout << "cool down in backstrokeswimming type" << std::endl; }
    void breathe(){  std::cout << "breathe   in backstrokeswimming type" << std::endl; }
    void armMove(){  std::cout << "arm move  in backstrokeswimming type" << std::endl; }
    void legMove(){  std::cout << "leg move  in backstrokeswimming type" << std::endl; }
};

int main(){
    BackStrokeSwimming b;
    b.armMove();
}

采用策略模式,将warmUp、coolDown等行为单独封装成类。子类不再”是“什么,而是”有“什么。

策略模式其实就是采用组合方式进行策略组合,一方面减少了重复的代码,另一方面可以更灵活地在运行时更换策略。

(一)Head first design patterns策略模式(c++)_第2张图片

示例代码:策略方式

#include

class IWarmUpMovementStrategy{
public:
    virtual void move(){ std::cout << "move in one default way" << std::endl; }
};
class IWarmUpS : public IWarmUpMovementStrategy{
public:
    virtual void move(){ std::cout << "warm up in a slow way" << std::endl; }
};
class IWarmUpQ : public IWarmUpMovementStrategy{
public:
    virtual void move(){ std::cout << "warm up in a quick way" << std::endl; }
};
class ICoolDownMovementStrategy{
public:
    virtual void move(){ std::cout << "move in one default way" << std::endl; }
};
class IBreatheMovementStrategy{
public:
    virtual void move(){ std::cout << "move in one default way" << std::endl; }
};
class IArmMovementStrategy{
public:
    virtual void move(){ std::cout << "move in one default way" << std::endl; }
};
class ILegMovementStrategy{
public:
    virtual void move(){ std::cout << "move in one default way" << std::endl; }
};
class ILegMoveS : public ILegMovementStrategy{
public:
    virtual void move(){ std::cout << "leg move in slow speed way" << std::endl; }
};
class ILegMoveH : public ILegMovementStrategy{
public:
    virtual void move(){ std::cout << "leg move in high spped way" << std::endl; }
};
class Swimming
{
public:
    Swimming(){
        mIWarmUpMovementStrategy    = new IWarmUpMovementStrategy   ; 
        mICoolDownMovementStrategy  = new ICoolDownMovementStrategy ; 
        mIBreatheMovementStrategy   = new IBreatheMovementStrategy  ; 
        mIArmMovementStrategy       = new IArmMovementStrategy      ; 
        mILegMovementStrategy       = new ILegMovementStrategy      ; 
    }
    virtual void warmUp()  { mIWarmUpMovementStrategy->move(); }
    virtual void coolDown(){ mICoolDownMovementStrategy->move(); }
    virtual void breathe() { mIBreatheMovementStrategy->move(); }
    virtual void armMove() { mIArmMovementStrategy->move(); }
    virtual void legMove() { mILegMovementStrategy->move(); }
    void setWarmUpMovementStrategy(IWarmUpMovementStrategy* s){
        mIWarmUpMovementStrategy = s;
    }
    void setCoolDownMovementStrategy(ICoolDownMovementStrategy* s){
        mICoolDownMovementStrategy = s;
    }
    void setBreatheMovementStrategy(IBreatheMovementStrategy* s){
        mIBreatheMovementStrategy = s;
    }
    void setArmMovementStrategy(IArmMovementStrategy* s){
        mIArmMovementStrategy = s;
    }
    void setLegMovementStrategy(ILegMovementStrategy* s){
        mILegMovementStrategy = s;
    }
    IWarmUpMovementStrategy       *mIWarmUpMovementStrategy;
    ICoolDownMovementStrategy     *mICoolDownMovementStrategy;
    IBreatheMovementStrategy      *mIBreatheMovementStrategy;
    IArmMovementStrategy          *mIArmMovementStrategy;
    ILegMovementStrategy          *mILegMovementStrategy;
};

class ButterflySwimming : public Swimming
{
};
class FreeStyleSwimming : public Swimming
{
};
class BreastStrokeSwimming : public Swimming
{
};
class BackStrokeSwimming : public Swimming
{
};

int main(){
    BackStrokeSwimming b;
    b.setWarmUpMovementStrategy(new IWarmUpS);
    b.setLegMovementStrategy(new ILegMoveS);
    b.warmUp();
    b.legMove();
    b.setLegMovementStrategy(new ILegMoveH);
    b.legMove();
}

你可能感兴趣的:(设计模式cpp,设计模式)