策略模式:定义了算法家族,分别封装起来,让它们之间可以相互转换,此模式让算法的变化,不会影响到使用算法的客户。
从概念上来看,所有的这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法。
优点:算法可以自由切换,扩展性好。
缺点:策略增多时,上层必须知道具体的策略。
适用于多个类只在算法上不同的地方。
用策略模式实现商场促销:
#include <iostream>
using namespace std;
class CashSuper
{
public:
virtual double acceptCash(double money) = 0;
};
class CashNormal : public CashSuper
{
public:
double acceptCash(double money)
{
return money;
}
};
class CashRebate : public CashSuper
{
public:
CashRebate(double moneyRebate)
{
_moneyRebate = moneyRebate;
}
double acceptCash(double money)
{
return _moneyRebate*money;
}
private:
double _moneyRebate;
};
class CashReturn : public CashSuper
{
public:
CashReturn(double moneyReturn,double moneyCondition)
{
_moneyReturn = moneyReturn;
_moneyCondition = moneyCondition;
}
double acceptCash(double money)
{
double result = 0.0;
if(money >= _moneyCondition)
result = money - (money / _moneyCondition)*_moneyReturn;
else
result = money;
return result;
}
private:
double _moneyReturn;
double _moneyCondition;
};
class CashContext
{
public:
CashContext(CashSuper* cs)
{
this->_cs = cs;
}
double GetResult(double money)
{
return _cs->acceptCash(money);
}
private:
CashSuper* _cs;
};
int main()
{
int count;
double price;
double total = 0.0;
int type;
cout << "请输入单价:";
cin >> price;
cout << "请输入数量:";
cin >> count;
cout << "请输入打折类型:";
cin >> type;
CashContext* oper = NULL;
switch(type)
{
case 1:
oper = new CashContext(new CashNormal);
break;
case 8:
oper = new CashContext(new CashRebate(0.8));
break;
case 300 - 100:
oper = new CashContext(new CashReturn(300,100));
break;
}
cout << oper->GetResult(price*count) << endl;
return 0;
}
通过之前学习的简单工厂模式,可以进一步优化商场促销的代码,利用简单工厂和策略模式共同实现:
#include <iostream>
using namespace std;
class CashSuper
{
public:
virtual double acceptCash(double money) = 0;
};
class CashNormal : public CashSuper
{
public:
double acceptCash(double money)
{
return money;
}
};
class CashRebate : public CashSuper
{
public:
CashRebate(double moneyRebate)
{
_moneyRebate = moneyRebate;
}
double acceptCash(double money)
{
return _moneyRebate*money;
}
private:
double _moneyRebate;
};
class CashReturn : public CashSuper
{
public:
CashReturn(double moneyReturn,double moneyCondition)
{
_moneyReturn = moneyReturn;
_moneyCondition = moneyCondition;
}
double acceptCash(double money)
{
double result = 0.0;
if(money >= _moneyCondition)
result = money - (money / _moneyCondition)*_moneyReturn;
else
result = money;
return result;
}
private:
double _moneyReturn;
double _moneyCondition;
};
class CashContext
{
public:
CashSuper* _cs = NULL;
CashContext(int type)
{
switch(type)
{
case 1:
_cs = new CashNormal;
break;
case 8:
_cs = new CashRebate(0.8);
break;
case 300 - 100:
_cs = new CashReturn(300,100);
break;
}
}
double GetResult(double money)
{
return _cs->acceptCash(money);
}
};
int main()
{
int count;
double price;
double total = 0.0;
int type;
cout << "请输入单价:";
cin >> price;
cout << "请输入数量:";
cin >> count;
cout << "请输入打折类型:";
cin >> type;
CashContext* oper = new CashContext(type);
cout << oper->GetResult(price*count) << endl;
return 0;
}