Design Patterns---- Strategy 模式(一)

2014-11-24 10:52:35 · 作者: · 浏览: 0

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

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


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

1. 如果采用之前将的工厂模式,可以采用如下的方式编写:

// Last Update:2014-03-17 21:05:53
/**
 * @file cash.h
 * @brief Strategy Pattern
 * @author shoulinjun@126.com
 * @version 0.1.00
 * @date 2014-03-17
 */

#ifndef CASH_H
#define CASH_H

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       // 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_; }; // factory class class CashFactory{ public: static CashSuper* createCashSuper(std::string type) { CashSuper *pCashSuper(NULL); 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{ return NULL; } return pCashSuper; } }; #endif /*CASH_H*/ #include "cash.h" #include 
      
        #include "boost/shared_ptr.hpp" using namespace std; int main(void) { boost::shared_ptr
       
         pCashSuper(CashFactory::createCashSuper("正常收费")); cout << pCashSuper->acceptCash(300) << endl; pCashSuper = boost::shared_ptr
        
         (CashFactory::createCashSuper("打8折")); cout << pCashSuper->acceptCash(300) << endl; pCashSuper = boost::shared_ptr
         
          (CashFactory::createCashSuper("满300减100")); cout << pCashSuper->acceptCash(300) << endl; return 0; }
         
        
       
      
     
    
   
  



2. Strategy 模式

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

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

// 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_;
};

class CashContext{
public:
    CashContext(std::string type): pCashSuper_(NULL){
        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
    }

    ~Ca