大话设计模式——策略模式(C++实现)

【大话设计模式——策略模式(C++实现)】用C++实现了《大话设计模式》中的策略模式(Strategy)


Strategy.h:

#ifndef _STRATEGY_H #define _STRATEGY_H #include using namespace std; class CCashBase { public: virtual double ReturnMoney(double money)=0; protected: private: }; class CCashNomal : public CCashBase { public: double ReturnMoney(double money); protected: private: }; class CCRebate : public CCashBase { public: CCRebate(double rebate):m_dMoneyRebate(rebate){}; double ReturnMoney(double money); protected: private: double m_dMoneyRebate; }; class CCReturn : public CCashBase { public: CCReturn(double moneyCondition,double moneyReturn) :m_dMoneyCondition(moneyCondition),m_dMoneyReturn(moneyReturn){}; double ReturnMoney(double money); protected: private: double m_dMoneyCondition,m_dMoneyReturn; }; //策略模式 class CCashContext1 { public: CCashContext1(CCashBase *cb); double GetResult(double money); protected: private: CCashBase *m_pCB; }; //策略与简单工厂模式结合 class CCashContext2 { public: CCashContext2(int i); double GetResult(double money); protected: private: CCashBase *m_pCB; }; #endif


Strategy.cpp:

#include "Strategy.h" /************************************************************************/ /* CCashNomal类*/ /************************************************************************/ double CCashNomal::ReturnMoney( double money ) { return money; } /************************************************************************/ /* CCRebate类*/ /************************************************************************/ double CCRebate::ReturnMoney( double money ) { return money * m_dMoneyRebate; } /************************************************************************/ /* CCReturn类*/ /************************************************************************/ double CCReturn::ReturnMoney( double money ) { double result = money; if (money >= m_dMoneyCondition) result = money - int(money/m_dMoneyCondition) * m_dMoneyReturn; return result; } /************************************************************************/ /* CCashContext1类*/ /************************************************************************/ CCashContext1::CCashContext1( CCashBase *cb ) { m_pCB = cb; }double CCashContext1::GetResult( double money ) { return m_pCB->ReturnMoney(money); } /************************************************************************/ /* CCashContext2类*/ /************************************************************************/ CCashContext2::CCashContext2( int i ) { switch(i) { case 1: m_pCB = new CCashNomal; break; case 2: m_pCB = new CCReturn(300,100); break; case 3: m_pCB = new CCRebate(0.8); break; default: m_pCB = new CCashNomal; }}double CCashContext2::GetResult( double money ) { return m_pCB->ReturnMoney(money); }


#include #include "Strategy.h" using namespace std; int main() { //策略模式 CCashContext1 *pCT1 = NULL; cout<<"请选择折扣:"<>i; switch (i) { case 1: pCT1 = new CCashContext1(new CCashNomal); break; case 2: pCT1 = new CCashContext1(new CCReturn(300,100)); break; case 3: pCT1 = new CCashContext1(new CCRebate(0.8)); break; default: pCT1 = new CCashContext1(new CCashNomal); } cout






    推荐阅读