简单记录一下策略模式,策略模式是一种定义一系列算法的方法,这些算法完成相同工作, 只是实现的细节不一样。那么我们可以利用策略模式, 封装算法, 实现算法与算法使用者之间的解耦工作。
另外, 策略模式利于单元检测, 因为各个算法之间是相互独立的。
下面上UML图
这个图和简单工厂模式的图很类似, 不过简单工厂模式中 工厂类 和 产品基类之间是一种依赖关系, 而这里是一种聚合关系。
代码如下:
Csuper.h
#ifndef _CASHSUPER_H_
#define _CASHSUPER_H_
template <class T>
class CCashSuper{
public:
/************************************************************************/
/* 收取现金的接口 */
/************************************************************************/
virtual double acceptCash(T money) const /*{ return money; }*/ = 0;
};
#endif // _CASHSUPER_H_
MainSuper.h
#ifndef _MAINCASHSTRATEGY_H_
#define _MAINCASHSTRATEGY_H_
#include "CashSuper.h"
/************************************************************************/
/* 正常收费的策略 */
/************************************************************************/
template<class T>
class CCashNormal : public CCashSuper<T>{
public:
T acceptCash(T money) const{
return money;
}
};
/************************************************************************/
/* 打折收费的策略 */
/************************************************************************/
template<class T, class U>
class CCashRebate : public CCashSuper<T>{
public:
CCashRebate(T debate) : debate(debate){}
// 操作 debate 数据
void setRebate(U debate){ this->debate = debate; }
U getRebate() const { return debate; }
// 计算收费
T acceptCash(T money) const{
return money * getRebate();
}
private:
U debate;
};
/************************************************************************/
/* 返现收费的策略 */
/************************************************************************/
template<class T, class U>
class CCashReturn : public CCashSuper<T>{
public:
CCashReturn(T moneyCond, T moneyRet) : moneyCond(moneyCond), moneyRet(moneyRet){}
// manipulate the moneyCond
U getMoneyCond() const { return moneyCond; }
void setMoneyCond(U val) { moneyCond = val; }
// manipulate the moneyRet
U getMoneyRet() const { return moneyRet; }
void setMoneyRet(U val) { moneyRet = val; }
// calc the money
T acceptCash(T money) const{
return money >= moneyCond ? money - moneyRet : money;
}
private:
U moneyCond; // 返现标准
U moneyRet; // 返现额度
};
#endif // _MAINCASHSTRATEGY_H_
strategyContext.h
#ifndef _STRATEGYCONTEXT_H_
#define _STRATEGYCONTEXT_H_
#include "MainCashStrategy.h"
#include <memory>
template <class T>
class CStrategyContext{
public:
CStrategyContext(int type){
switch (type)
{
case 0: // normal cash
strategy = std::shared_ptr<CCashSuper<T>>(new CCashNormal<T>());
break;
case 1: // rebate cash
strategy = std::shared_ptr<CCashSuper<T>>(new CCashRebate<T, double>(0.8));
break;
case 2: // return cash
strategy = std::shared_ptr<CCashSuper<T>>(new CCashReturn<T, double>(300.0, 100.0));
break;
default:
break;
}
}
T getResult(T money) const {
return strategy->acceptCash(money);
}
private:
std::shared_ptr<CCashSuper<T>> strategy;
};
#endif // _STRATEGYCONTEXT_H_
main.cpp
#include <iostream>
#include <memory>
#include "StrategyContext.h"
using namespace std;
int main(){
shared_ptr<CStrategyContext<double>> context(new CStrategyContext<double>(2));
cout << context->getResult(300.0) << endl;
system("pause");
return 0;
}