博客中的例题及构建思想均来自于程杰老师的“大话设计模式”。
策略模式和简单工厂模式在实现上没有太大的区别。策略模式相对于工厂模式最大的优点就是将各个算法策略都进一步封装在了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;
}