#include <iostream>
using namespace std;
//
struct ITravelWay
{
public:
virtual ~ITravelWay() { }
virtual void Travel() = 0;
};
class CTrainWay : public ITravelWay
{
public:
virtual ~CTrainWay() { }
virtual void Travel() { cout<<"Travel by train."<<endl; }
};
class CAirplaneWay : public ITravelWay
{
public:
virtual ~CAirplaneWay() { }
virtual void Travel() { cout<<"Travel by airplane."<<endl; }
};
class CBicycleWay : public ITravelWay
{
public:
virtual ~CBicycleWay() { }
virtual void Travel() { cout<<"Travel by bicycle."<<endl; }
};
class CTravel
{
public:
void Travel(ITravelWay *travelWay) { travelWay->Travel(); }
};
//
void main()
{
CTravel travel;
CTrainWay train;
travel.Travel(&train);
CAirplaneWay airplane;
travel.Travel(&airplane);
CBicycleWay bicycle;
travel.Travel(&bicycle);
}
设计模式14:策略模式【行为型】
最新推荐文章于 2013-09-13 15:26:16 发布