大话设计模式之策略模式C++实现

博客中的例题及构建思想均来自于程杰老师的“大话设计模式”。
策略模式和简单工厂模式在实现上没有太大的区别。策略模式相对于工厂模式最大的优点就是将各个算法策略都进一步封装在了Context对象中。客户端只需要认识一个类Context就行了,进一步降低了耦合度。

实现商场打折促销:(简单工厂和策略模式)

#include<iostream>

using namespace std;

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

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

class CashRebate :public CashSuper {
private:
	double moneyRebate;
public:
	CashRebate() {
		cout << "Please enter money rebate ratio: ";
		if (!(cin >> moneyRebate)) {
			throw "Money rebate ratio mush be a number!\n";
			exit(EXIT_FAILURE);
		}
		else if (moneyRebate > 1) {
			throw "Money rebate ratio mush between 0 and 1!";
			exit(EXIT_FAILURE);
		}
	}

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

class CashReturn :public CashSuper {
private:
	double moneyCondition;
	double moneyReturn;
public:
	CashReturn(){
		cout << "Please enter money condition and money return: ";
		if (!(cin >> moneyCondition >> moneyReturn)) {
			throw "Money rebate ratio mush be a number!\n";
			exit(EXIT_FAILURE);
		}
		else if (moneyReturn > moneyCondition) {
			throw "money return is larger than money condition!";
			exit(EXIT_FAILURE);
		}
	}

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

class CashFactory {
public:
	CashSuper* creatCashAccept() {
		int choice;
		cout << "0 -> CashNormal  1 -> CashRebate  2 -> CashReturn\n";
		cout << "Please choose count models: ";
		cin >> choice;
		CashSuper* cs = nullptr;
		switch (choice)
		{
		case 0:
			cs = new CashNormal;
			break;
		case 1:
			cs = new CashRebate;
			break;
		case 2:
			cs = new CashReturn;
			break;
		default:
			throw "The operands are incorrect!";
			break;
		}
		return cs;
	}
};

class CashContext {
private:
	CashSuper* cs = nullptr;
public:
	CashContext() {
		cout << "0 -> CashNormal  1 -> CashRebate  2 -> CashReturn\n";
		cout << "Please choose count models: ";
		int choice;
		cin >> choice;
		switch (choice)
		{
		case 0:
			cs = new CashNormal;
			break;
		case 1:
			cs = new CashRebate;
			break;
		case 2:
			cs = new CashReturn;
			break;
		default:
			throw "The operands are incorrect!";
			break;
		}
	}

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

int main() {
	/*   简单工厂模式实现代码   */
	//CashSuper *cas = nullptr;
	//CashFactory fac;

	//try {
	//	cas = fac.creatCashAccept();
	//	cout << "Please enter the unit price: ";
	//	double price, number;
	//	cin >> price;
	//	cout << "Please enter the quantity of goods: ";
	//	cin >> number;
	//	double totalPrices = price * number;
	//	double countPrices = cas->acceptCash(totalPrices);
	//	cout << "The original price is: " << totalPrices << " ,and the discount price is: " << countPrices << endl;
	//	delete cas;
	//}
	//catch(const char* err){
	//	cerr << err << endl;
	//}

	/*   策略模式实现代码   */
	try {
		CashContext cash;
		cout << "Please enter the unit price: ";
		double price, number;
		cin >> price;
		cout << "Please enter the quantity of goods: ";
		cin >> number;
		double totalPrices = price * number;
		double countPrices = cash.acceptCash(totalPrices);
		cout << "The original price is: " << totalPrices << " ,and the discount price is: " << countPrices << endl;
	}
	catch (const char* err) {
		cerr << err << endl;
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值