设计模式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;
}

如需转载,请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值