您的位置:首页 > 编程语言 > C语言/C++

设计模式C++实现二:策略模式

2015-05-09 16:41 337 查看
策略模式(strategy): 定义算法家族,分别封装起来,让这些算法直接可以相互替换,我们可以自由添加或者修改算法而不会影响客户.

优点:简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。

如果我们在客户端为了判断使用哪个算法而使用switch语句来分析,我们可以使用策略模式把这个判断的过程隐藏到后台,把每个算法用一个strategy类实现。这样就简化了客户端的代码,也隐藏了实现的细节。

#ifndef STRATEGY_H
#define STRATEGY_H
#include<iostream>
using namespace std;
class CashSuper
{
friend class CashContext;
protected:
virtual double acceptCash(double money) = 0;
};

/*工厂模式
class CashContext
{
CashSuper *cs;
public:
CashContext(CashSuper *cashs) :cs(cashs){}
void operator =(CashSuper * re)
{
cs = re;
}
double GetResult(double money)
{
return cs->acceptCash(money);
}
};*/
class CashContext
{
CashSuper *cs;
char StrategyType;
public:
CashContext(){}
CashContext(char &SType);
void operator =(char &SType);
double GetResult(double money)
{
if (cs == NULL){ cout << "The strategy is unfind."; return -1; }
return cs->acceptCash(money);
}
};
class CashNormal:public CashSuper
{
//friend class CashContext;
public:
double acceptCash(double money)
{
return money;
}
};

class CashRebate:public CashSuper
{
//friend class CashContext;
double Rebate;
public:
CashRebate(double discount) :Rebate(discount){}
double acceptCash(double money)
{
return money*Rebate;
}
};

class CashReturn :public CashSuper
{
//friend class CashContext;
double baseCash,returnCash;
public:
CashReturn(double baseC,double returnC) :baseCash(baseC),returnCash(returnC) {}
double acceptCash(double money)
{
if (money >= baseCash) return money - returnCash;
return money;
}
};

CashContext::CashContext(char &SType) :StrategyType(SType)
{
switch (StrategyType)
{
case('a') :
case('A') :
cs = new CashNormal;
break;
case('b') :
case('B') :
cs = new CashRebate(0.8);
break;
case('c') :
case('C') :
cs = new CashReturn(1000, 200);
break;
default:
cs = NULL;
break;
}
}
void CashContext::operator =(char &SType)
{
StrategyType = SType;
switch (StrategyType)
{
case('a') :
case('A') :
cs = new CashNormal;
break;
case('b') :
case('B') :
cs = new CashRebate(0.8);
break;
case('c') :
case('C') :
cs = new CashReturn(1000, 200);
break;
default:
cs = NULL;
break;
}
}

#endif
#include"strategy.h"
int main()
{
double ShouldPay = 1520;
/*   简单工厂模式的实现,下面的三种策略的细节暴露在客户端
CashNormal cashn;
CashRebate cashrb(0.95);
CashReturn cashrt(1000, 200);
CashContext cashC(&cashn);
cout << "The real pay is : " << cashC.GetResult(ShouldPay) << endl;
cashC=&cashrb;
cout << "The real pay is : " << cashC.GetResult(ShouldPay) << endl;
cashC=&cashrt;
cout << "The real pay is : " << cashC.GetResult(ShouldPay) << endl;
*/
//下面实现的是用策略模式实现的,其付款细节在客户端是不可见的,如果要添加或者删除策略
//我们可以在strategy.h中添加该策略类,然后把这个类添加到CashContext中实现,这样在客户端
//很少要修改信息,实现细节的隐藏
cout << "Please enter the strategy type,'a' or 'A' instand the normalcash ,\n"
<< "'b' or 'B' instand the rebatecash, 'c' or 'C' instand the returncash.\n";
char type;
CashContext cashC;
while (cin >> type)
{
cashC = type;
cout << "The real pay is : " << cashC.GetResult(ShouldPay) << endl;

}
return 0;
}


如需转载,请注明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: