Head First strategy model

definition

The strategy mode defines a family of algorithms and encapsulates them separately so that they can be replaced with each other. This mode makes the algorithm changes independent of the customers who use the algorithm.

Encapsulated flight behavior

//飞行行为接口类
class Flybehavior
{
public:
    Flybehavior(){}
    //删除基类,所以这里析构函数要用虚函数
    virtual ~Flybehavior(){}
    virtual void fly() = 0;
};

//具体的飞行类行为
class FlyWithWings :public Flybehavior
{
public:
    void fly()
    {
        qDebug()<< "Fly With Wings";
    }
};
//具体的飞行类行为
class FlyNoWay :public Flybehavior
{
public:
    void fly()
    {
        qDebug()<< "Fly No Way";
    }
};
//具体的飞行类行为
class FlyWithRock :public Flybehavior
{
public:
    void fly()
    {
        qDebug()<< "Fly With Rock";
    }
};

Encapsulated croaking behavior

//呱呱叫行为接口类
class QuackBehavior
{
public:
    QuackBehavior(){}
    //删除基类,所以这里析构函数要用虚函数
    virtual ~QuackBehavior(){}
    virtual void quack() = 0;
};
//具体叫的行为类
class Quack:public QuackBehavior
{
public:
    void quack()
    {
        qDebug()<< "Quack";
    }
};
//具体叫的行为类
class Squeak:public QuackBehavior
{
public:
    void quack()
    {
        qDebug()<< "i can Squeak";
    }
};
//具体叫的行为类
class MuteQuack:public QuackBehavior
{
public:
    void quack()
    {
        qDebug()<< "Mute Quack";
    }
};

Customer class

class Duck
{
public:
    Duck():quackBehavior(0),flybehavior(0){}
    void swim();
    void display();

    //行为接口
    void performQauck();
    void performFly();

    //替换算法接口
    void setQauckBehavior(QuackBehavior*);
    void setFlyBehavior(Flybehavior*);

protected:
    //使用组合方式,而不是继承
    QuackBehavior *quackBehavior;
    Flybehavior * flybehavior;
};

void Duck::performQauck()
{
    quackBehavior->quack();//委托给QuackBehavior类实现
}

void Duck::performFly()
{
    flybehavior->fly();//委托给Flybehavior类实现
}

void Duck::setQauckBehavior(QuackBehavior *q)
{
    if(quackBehavior)
        delete quackBehavior;//这里要求基类的析构函数是虚函数

    quackBehavior = q;
}

void Duck::setFlyBehavior(Flybehavior *f)
{
    if(flybehavior)
        delete flybehavior;

    flybehavior = f;
}

class MallardDuck :public Duck
{
public:
    MallardDuck()
    {
        quackBehavior = new Squeak();
        flybehavior = new FlyWithWings();
    }

    void swim()
    {
        qDebug() << "i am mallard duck ,i can swim";
    }
    void display()
    {
         qDebug() << "i am mallard duck";
    }
};

Client code

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //父类指针指向子类实例
    Duck *duck1 = new MallardDuck();

    //对接口编程
    duck1->performFly();
    duck1->performQauck();
    duck1->setFlyBehavior(new FlyWithRock);
    duck1->performFly();
    duck1->performFly();

    return a.exec();
}

Static class diagram

Insert picture description here

Design Principles

  • Use more combination, less inheritance
  • Programming for the interface, not for the implementation (programming for the superclass)
  • Find out what may need to be changed in the application, isolate them, and don't mix them with the code that does not change

Guess you like

Origin blog.csdn.net/amwha/article/details/88211339
Recommended