#include "stdafx.h"
#include <iostream>
using namespace std;
//策略模式封装了变化。
// 策略模式就是用来封装算法的,但是在实践中,我们发现可以可以用它来封装几乎
//
// 任何类型的规则,只要在分析过程中听到需要在不同时间应用不同的业务规则,可
//
// 以考虑使用策略模式处理这种变化的可能性
class Stategy
{
public:
Stategy(void);
virtual ~Stategy(void);
//抽象算法接口
virtual void AlgorithmInterface()=0;
};
class StategyA :
public Stategy
{
public:
void AlgorithmInterface()
{
cout<<"算法A"<<"\n";
}
};
class StategyB :
public Stategy
{
public:
void AlgorithmInterface()
{
cout<<"算法B"<<"\n";
}
};
class StategyC :
public Stategy
{
public:
void AlgorithmInterface()
{
cout<<"算法C"<<"\n";
}
};
class MyContext
{
private:
Stategy*sgy;
public:
MyContext(int stategyType)
{
switch (stategyType)
{
case 1:
this->sgy = new StategyA();
break;
case 2:
this->sgy = new StategyB();
break;
case 3:
this->sgy = new StategyC();
break;
}
}
~MyContext(void)
{
}
void ContextInterface()
{
this->sgy->AlgorithmInterface();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyContext*con;
con = new MyContext(1);
con->ContextInterface();
con = new MyContext(2);
con->ContextInterface();
con = new MyContext(3);
con->ContextInterface();
int dc = 0;
return 0;
}