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3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">#ifndef STRATEGY_H
#define STRATEGY_H
#include
(2)策略:CashContext.h
#ifndef CASHCONTEXT_H #define CASHCONTEXT_H #include "Strategy.h" //策略類 class CashContext { private: CashSuper* cs; public: CashContext(int type):cs(NULL) { switch(type) { case 1: { CashSuper* cn=new CashNormal(); cs=cn; break; } case 2: { CashSuper* cr1=new CashReturn(300,100); cs=cr1; break; } case 3: { CashSuper* cr2=new CashRebate(0.8); cs=cr2; break; } default:; } } double GetResult(double money) { return cs->acceptCash(money); } }; #endif
(3)客戶端:main.h
#include "CashContext.h" #include#include void main() { double total=0; double totalPrices=0; //正常收費 CashContext* cc1=new CashContext(1); totalPrices=cc1->GetResult(300); total+=totalPrices; std::cout<<"Type:正常收費 totalPrices:"< GetResult(700); total+=totalPrices; std::cout<<"Type:滿300返100 totalPrices:"< GetResult(300); total+=totalPrices; std::cout<<"Type:打8折 totalPrices:"<