C++实现 策略模式(1) - 行为型


#include "stdafx.h"
#include <iostream>
using namespace std;


/**
* 策略模式: 它定义了算法家族, 分别封装起来,让它们之间可以互相替换,
* 此模式让算法的变化,不会影响到使用算法的客户。
**/


class CashSuper
{
public:
	virtual double acceptCash(double money){ return 0; }
};

// 正常收费
class CashNormal : public CashSuper
{
public:
	virtual double acceptCash(double money) override
	{
		return money;
	}
};

// 打折收费
class CashRebate : public CashSuper
{
private:
	double _moneyReabate = 1;

public:
	CashRebate(string moneyRebate)
	{
		_moneyReabate = atof(moneyRebate.c_str());
	}

public:
	virtual double acceptCash(double money) override
	{
		return money * _moneyReabate;
	}
};

// 返利收费
class CashReturn : public CashSuper
{
private:
	double _moneyCondition = 1.0;
	double _moneyReturn = 1.0;

public:
	CashReturn(string mCondition, string mReturn)
	{
		_moneyCondition = atof(mCondition.c_str());
		_moneyReturn = atof(mReturn.c_str());
	}

public:
	virtual double acceptCash(double money) override
	{
		cout << "money=" << money << endl;
		cout << "_moneyCondition=" << _moneyCondition << endl;
		cout << "_moneyReturn=" << _moneyReturn << endl;

		double result = money;
		if ((money-_moneyCondition) >= 0.0001)
		{
			result = money - (int)((money / _moneyCondition))*_moneyReturn;
		}
		return result;
	}
};
class CashContext
{
private:
	CashSuper*	cs;
	double			_rebate = 0.8;
	double			_moneyCondition = 300;
	double			_moneyReturn = 100;

private:
	void CashContext_normal()
	{
		cs = new CashNormal();
	}
	void CashContext_Return(string Condition, string Return)
	{
		cs = new CashReturn(Condition, Return);
	}
	void CashContext_Rebate(string rebate)
	{
		cs = new CashRebate(rebate);
	}

public:
	// type = 'n', 其他参数无效
	// type = 'r'  parm1=满额, parm2=减额,
	// type = 'z'  parm1=折扣率,
	CashContext(string type, string parm1 = "1", string parm2 = "0")
	{
		switch (type[0])
		{
		case 'n': // 正常收费
			CashContext_normal();
			break;
		case 'r': // 满返
			CashContext_Return(parm1, parm2);
			break;
		case 'z': // 打折扣
			CashContext_Rebate(parm1);
			break;
		default:
			cs = nullptr;
			break;
		}
	}
	
	void SetRebate(double rebate){ _rebate = rebate; }
	void SetReturn(double	 mCondition, double mReturn)
	{ 
		_moneyCondition = mCondition;
		_moneyReturn = mReturn;
	}

public:
	double GetResult(double money)
	{
		return cs->acceptCash(money);
	}
};


int _tmain(int argc, _TCHAR* argv[])
{

	CashContext *cc = new CashContext("r","300","50");
	double totalPrices = 888;
	double result = cc->GetResult(totalPrices);
	cout << "总计:" << totalPrices << ", 打折后总计:" << result << endl;
	totalPrices = result;

	cc = new CashContext("z","0.1");
	result = cc->GetResult(totalPrices);
	cout << "总计:" << totalPrices << ", 打折后总计:" << result << endl;

	return 0;
}

运行结果如下 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WendyWJGu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值