Design Patterns---- Strategy 模式

时间:2023-01-12 22:13:31

设计模式:可复用面向对象软件的基础 书中对 Strategy 模式的定义如下:

定义了一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于它的用户而变化。

案例:设计一个商场收银软件,营业员根据客户所购买商品的单价和数量,向客户收费。商场有许多促销的策略,打折,满300减100等。。。

策略模式可以减少各种算法类与使用算法类之间的耦合;

简化单元测试,因为每个算法都有自己的类,可以通过自己的接口进行单元测试。

注意: 下面代码中用一个 CashContext 的 智能指针类来保存 基类的指针。。

#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cassert> /* abstract base class
* define interface
*/
class CashSuper
{
public:
virtual ~CashSuper() {}
virtual double acceptCash(double money) = 0;
}; class CashNormal : public CashSuper
{
public:
double acceptCash(double money){
return money;
}
}; class CashRebate : public CashSuper{
public:
CashRebate(std::string moneyRate = "0.0"){
std::stringstream s(moneyRate);
s >> this->moneyRate_;
} double acceptCash(double money){
return money * moneyRate_;
}
private:
double moneyRate_;
}; class CashReturn : public CashSuper{
public:
CashReturn(std::string moneyCondition, std::string moneyReturn){
std::stringstream s(moneyCondition);
s >> this->moneyCondition_;
std::stringstream s2(moneyReturn);
s2 >> this->moneyReturn_;
} double acceptCash(double money){
if(money >= moneyCondition_){
money -= std::floor(money / moneyCondition_) * moneyReturn_;
}
return money;
}
private:
double moneyCondition_, moneyReturn_;
}; /* handler class
* smart pointer
*/
class CashContext{
public:
CashContext(std::string type)
: pCashSuper_(NULL), use(new std::size_t(1))
{
if(type == "正常收费"){
pCashSuper_ = new CashNormal;
}
else if(type == "满300减100"){
pCashSuper_ = new CashReturn("300", "100");
}
else if(type == "打8折"){
pCashSuper_ = new CashRebate("0.8");
}
// else
} // copy control
CashContext(const CashContext &rhs)
: pCashSuper_(rhs.pCashSuper_), use(rhs.use)
{
++ *use;
}
// deal with self-assignment
CashContext& operator=(const CashContext &rhs){
++ *(rhs.use);
if(-- *use == 0){
delete pCashSuper_;
delete use;
}
pCashSuper_ = rhs.pCashSuper_;
use = rhs.use;
return *this;
} ~CashContext(){
if(-- *use == 0){
delete pCashSuper_;
delete use;
}
} double GetResult(double money){
assert(pCashSuper_ != NULL);
return pCashSuper_->acceptCash(money);
}
private:
CashSuper *pCashSuper_;
std::size_t *use;
};

参考资料:

1. 大话设计模式

2. 设计模式:可复用面向对象软件的基础