`

策略(stragety)模式

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

设计原则:封装变化,针对接口编程



Stragety.h
#ifndef STRAGETY_H
#define STRAGETY_H
#include<iostream>
using namespace std;

class FlyBehavior{
public:
    virtual void fly()const=0;
};

class FlyWithWings:public FlyBehavior{
public:
    void fly()const{
        cout << "i can fly!" << endl;
    }
};

class FlyNoWay:public FlyBehavior{
public:
    void fly()const{
        cout << "i can't fly!" << endl;
    }
};

class FlyRocketPower:public FlyBehavior{//火箭动力器
public:
    void fly()const{
        cout << "i can fly by rocket power" << endl;
    }
};

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

class Quack:public QuackBehavior{
public:
    void quack()const{
        cout << "quack" << endl;//呱呱叫
    }
};

class Squeak:public QuackBehavior{
public:
    void quack()const{
        cout << "squeak" << endl;//吱吱叫
    }
};

class MuteQuack:public QuackBehavior{
public:
    void quack()const{
        cout << "can't quack" << endl;
    }
};

//普通鸭
class Duck{
public:
    Duck();
    virtual ~Duck();
    virtual void swim()const;
    virtual void display()const;
    virtual void performQuack()const;
    virtual void performFly()const;
    virtual void setFlyBehavior(FlyBehavior* fb);
private:
    QuackBehavior* quackBhr;
    FlyBehavior* flyBhr;
};

//塑料鸭
class PlasticDuck:public Duck{
public:
    PlasticDuck();
    ~PlasticDuck();
    void swim()const;
    void display()const;
    void performQuack()const;
    void performFly()const;
    void setFlyBehavior(FlyBehavior *fb);
private:
    QuackBehavior* quackBhr;
    FlyBehavior* flyBhr;
};

#endif // STRAGETY_H


Stragety.cpp
#include"Stragety.h"

Duck::Duck(){
    quackBhr = new Quack;
    flyBhr = new FlyNoWay;
}

Duck::~Duck(){}

void Duck::swim()const{
    cout << "duck is swimming!" << endl;
}

void Duck::display()const{
    cout << "i'm a duck" << endl;
}

void Duck::performFly()const{
    flyBhr->fly();
}

void Duck::performQuack()const{
    quackBhr->quack();
}

void Duck::setFlyBehavior(FlyBehavior* fb){
    flyBhr = fb;
}

PlasticDuck::PlasticDuck(){
    quackBhr = new MuteQuack;
    flyBhr = new FlyNoWay;
}

PlasticDuck::~PlasticDuck(){}

void PlasticDuck::display()const{
    cout << "i am a plastic duck" << endl;
}

void PlasticDuck::swim()const{
    cout << "plastic duck is swimming" << endl;
}

void PlasticDuck::performFly()const{
    flyBhr->fly();
}

void PlasticDuck::performQuack()const{
    quackBhr->quack();
}

void PlasticDuck::setFlyBehavior(FlyBehavior *fb){
    flyBhr = fb;
}

int main()
{
    Duck *d = new Duck;
    PlasticDuck *p = new PlasticDuck;
    cout <<  "-----common duck-----:" << endl;
    d->display();
    d->swim();
    d->performFly();
    d->performQuack();
    cout << "-----plastic duck-----:" << endl;
    p->display();
    p->swim();
    p->performFly();
    p->performQuack();
    cout << "-----after add rocket power-----:" << endl;
    p->setFlyBehavior(new FlyRocketPower);
    p->display();
    p->performFly();

}


-----common duck-----:
i'm a duck
duck is swimming!
i can't fly!
quack
-----plastic duck-----:
i am a plastic duck
plastic duck is swimming
i can't fly!
can't quack
-----after add rocket power-----:
i am a plastic duck
i can fly by rocket power
  • 大小: 84.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics