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

策略模式(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;
}

如需转载,请注明出处。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。