大话设计模式(C++)第二章-策略模式

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

三、优点与解析

(1)策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,他可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。

(2)策略模式的Strategy类曾是为Context定义了一些列的可供重用的算法或行为。集成有助于析取出这些算法中的公共功能。

(3)策略模式简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。

(4)策略模式就是用来封装算法的。

(5)只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性。

(6)简单工厂模式需要让客户端认识两个类,而策略模式和简单工厂模式结合的用法,客户端只需要认识一个类Context即可。

C++实现:

#include<iostream>
#include<cmath>
#include<cstdlib>

using namespace std;

class CashSuper   //抽象类
{
public:
	virtual double acceptCash(double money) = 0;

};

//子类,具体的实现,每种付费类型,正常付费类型
class CashNormal :public CashSuper
{
public:
	double acceptCash(double money)
	{
		return money;
	}

};

//返现类型
class CashReturn :public CashSuper
{
private:
	double moneyCondition;
	double moneyReturn;

public:
	CashReturn(double moneyCondition, double moneyReturn)
	{
		this->moneyCondition = moneyCondition;
		this->moneyReturn = moneyReturn;
	}

	double acceptCash(double money)
	{
		double result = money;
		if (money > moneyCondition)
			result = money - floor(money / moneyCondition) * moneyReturn;
		return result;
	}
};

//打折扣类型
class CashRebate :public CashSuper
{
private:
	double moneyRebate;

public:
	CashRebate(double moneyRebate)
	{
		this->moneyRebate = moneyRebate;
	}

	double acceptCash(double money)
	{
		return money*moneyRebate;
	}
};

//-------------------------------------------------分割线---------------------------------//
//策略与简单工厂模式结合的
class CashContext
{
private:
	CashSuper *cs;

public:
	CashContext(int type) :cs(nullptr)   //在构造函数中直接实例化
	{
		switch (type)
		{
		case 1:
		{
			cs = new CashNormal();
			break;
		}
		case 2:
		{
			cs = new CashReturn(300, 100);
			break;
		}
		case 3:
		{
			cs = new CashRebate(0.8);
			break;
		}
		default:
			break;
		}
	}


	~CashContext()  //在析构函数中将构造函数中,实例化的对象释放
	{
		if (cs != nullptr)
		{
			delete cs;
			cs = nullptr;
		}
	}

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

};


//-------------------------------------------------分割线---------------------------------//
//客户端

int main()
{
	double total = 0;
	double totalPrices = 0;

	//正常收费
	CashContext* cc1 = nullptr;
	cc1 = new CashContext(1);
	totalPrices = cc1->GetResult(300);
	total += totalPrices;
	cout << "Type: 正常收费 totalPrices:" << totalPrices << " total:" << total << endl;
	totalPrices = 0;

	//返现类型
	CashContext *cc2 = nullptr;
	cc2 = new CashContext(2);
	totalPrices = cc2->GetResult(700);
	total += totalPrices;
	cout << "Type:满300返100 totalPrices:" << totalPrices << " total:" << total << endl;
	totalPrices = 0;

	//打折类型
	CashContext *cc3 = nullptr;
	cc3 = new CashContext(3);
	totalPrices = cc3->GetResult(300);
	total += totalPrices;
	cout << "Type:打8折 totalPrices:" << totalPrices << " total:" << total << endl;
	totalPrices = 0;

	if (cc1 != nullptr)
	{
		delete cc1;
		cc1 = nullptr;
	}
	if (cc2 != nullptr)
	{
		delete cc2;
		cc2 = nullptr;
	}
	if (cc3 != nullptr)
	{
		delete cc3;
		cc3 = nullptr;
	}

	//while (1);
	return 0;
}

本文转自:
作者:西青年 
来源:CSDN 
原文:https://blog.csdn.net/xiqingnian/article/details/41855391 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/Rage_/article/details/84255703