设计模式之策略模式

设计模式的其中三个设计原则:

1. Program to an interface, not an implementation.        接口与实现

2. Favor composition over inheritance.                           组合与继承

3. Encapulate what varies.

 

下面是这一章的实现(in C++)

// Design-Pattern.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

/************************************************************************/
/* fly interface                                                                     */
/************************************************************************/
class FlyBehavior
{
public:
    virtual void fly() = 0;
};

/************************************************************************/
/* fly with wings                                                                     */
/************************************************************************/
class FlyWithWings: public FlyBehavior
{
public:
    void fly()
    {
        cout<<"Duck flying"<<endl;
    }
};

/************************************************************************/
/* fly no way                                                                     */
/************************************************************************/
class FlyNoWay: public FlyBehavior
{
public:
    void fly()
    {
        cout<<"Duck can't fly."<<endl;
    }
};

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

/************************************************************************/
/* quack Implementation from quack behavior                                                                    */
/************************************************************************/
class Quack: public QuackBehavior
{
public:
    void quack()
    {
        cout<<"Duck quacking"<<endl;
    }
};


/************************************************************************/
/* squeak implementation from quack behavior                                                                     */
/************************************************************************/
class Squeak: public QuackBehavior
{
public:
    void quack()
    {
        cout<<"Duck squeaking"<<endl;
    }
};

/************************************************************************/
/* quack no way, implementation from quack behavior                                                                     */
/************************************************************************/
class MuteQuack: public QuackBehavior
{
public:
    void quack()
    {
        cout<<"Duck can't quack."<<endl;
    }
};

/************************************************************************/
/* abstract class Duck                                                                     */
/************************************************************************/
class Duck
{
protected:
    FlyBehavior *flyBehavior;
    QuackBehavior *quackBehavior;

public:
    void setFlyBehavior(FlyBehavior *fly)
    {
        flyBehavior = fly;
    }

    void setQuackBehavior(QuackBehavior *quack)
    {
        quackBehavior = quack;
    }

    void performQuack()
    {
        quackBehavior->quack();
    }

    void performFly()
    {
        flyBehavior->fly();
    }

    void swim()
    {
        cout<<"Duck::swim()"<<endl;
    }

    virtual void display() = 0;
};

class MallardDuck: public Duck
{
public:
    MallardDuck()
    {
        flyBehavior = new FlyNoWay(); 
        quackBehavior = new Quack();
    }

    ~MallardDuck()
    {
        if(!flyBehavior)
            delete flyBehavior;
        if(!quackBehavior)
            delete quackBehavior;
    }

    void display()
    {
        cout<<"I'am a MallardDuck."<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Duck *duck = new MallardDuck();
    duck->display();
    duck->performFly();
    duck->performQuack();
    duck->setFlyBehavior(new FlyWithWings());
    duck->performFly();
    system("pause");
    return 0;
}

 

posted @ 2015-03-04 17:17  wiessharling  阅读(142)  评论(0编辑  收藏  举报