策略模式(Strategy) :
它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户。
结构图:
//Strategy抽象类,定义所有支持的算法的公共接口。
class Strategy
{
public:
Strategy(){};
//算法方法
virtual void AlgorithmInterface(){};
};
//封装了具体算法或行为,继承于Strategy
//具体算法A
class ConcreteStrategyA : public Strategy
{
public:
ConcreteStrategyA(){};
//算法A实现方法
virtual void AlgorithmInterface()
{
cout << "算法A实现方法" << endl;
}
};
//具体算法B
class ConcreteStrategyB : public Strategy
{
public:
ConcreteStrategyB(){};
//算法B实现方法
virtual void AlgorithmInterface()
{
cout << "算法B实现方法" << endl;
}
};
//具体算法C
class ConcreteStrategyC : public Strategy
{
public:
ConcreteStrategyC(){};
//算法C实现方法
virtual void AlgorithmInterface()
{
cout << "算法C实现方法" << endl;
}
};
//Context:用一个ConcreteStrategy来配置,维护一个对Strategy对象的引用
class Context
{
private:
Strategy * m_strategy;
public:
Context(){};
//初始化时,传入具体的策略对象
Context(Strategy * strategy):m_strategy(strategy)
{
}
//根据具体的策略对象,调用其算法的方法
void ContextInterface()
{
m_strategy->AlgorithmInterface();
}
};
//客户端代码
int main()
{
Context * context;
Strategy * m_strategy = new ConcreteStrategyA();
context = new Context(m_strategy);
context->ContextInterface();
delete context;
delete m_strategy;
m_strategy = new ConcreteStrategyB();
context = new Context(m_strategy);
context->ContextInterface();
delete context;
delete m_strategy;
m_strategy = new ConcreteStrategyC();
context = new Context(m_strategy);
context->ContextInterface();
delete context;
delete m_strategy;
return 0;
}
策略模式实现
#include "stdafx.h"
#include "stdio.h"
#include <memory>
using namespace std;
// 一个超市打折的实例
//抽象类
class CashSuper
{
public:
virtual double acceptCash(double money) = 0;
};
//正常收费子类
class CashNormal : public CashSuper
{
public:
virtual double acceptCash(double money)
{
return money;
}
};
//打折收费子类
class CashRebate : public CashSuper
{
private:
double moneyRebate;
public:
CashRebate(double Rebate = 0.0):moneyRebate(Rebate)
{
}
virtual double acceptCash(double money)
{
return money * moneyRebate;
}
};
//返利收费子类
class CashReturn : public CashSuper
{
private:
double moneyCondition;
double moneyReturn;
public:
CashReturn(double Condition = 0.0, double Return = 0.0):moneyCondition(Condition),
moneyReturn(Return)
{
}
virtual double acceptCash(double money)
{
double result = money;
if(money >= moneyCondition)
result = money - (money / moneyCondition) * moneyReturn;
return result;
}
};
class CashContext
{
private:
auto_ptr<CashSuper> cs;
public:
CashContext(){};
//简单工厂模式和策略模式融合到一起
void createCashAccept(int type)
{
switch(type)
{
//正常收费
case 0:
cs = auto_ptr<CashSuper> (new CashNormal());
break;
case 1:
cs = auto_ptr<CashSuper> (new CashReturn(300, 100));
break;
case 2:
cs = auto_ptr<CashSuper> (new CashRebate(0.8));
break;
}
}
double GetResult(double money)
{
return cs->acceptCash(money);
}
};
int main(int argc, char* argv[])
{
CashContext * cs = new CashContext();
cs->createCashAccept(0);
printf("%2.2f/n",cs->GetResult(1000));
delete cs;
return 0;
}
总结:
策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。策略模式的Strategy类层次为Context定义了一系列的可供重用的算法行为。继承有助于析取出这些算法的公有功能。
另外一个策略模式的优点是简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。