Policy mode (strategy): Define the algorithm family, separately encapsulated, so that these algorithms can directly replace each other, we can freely add or modify the algorithm without affecting the customer.
Pros: Simplifies unit testing because each algorithm has its own class that can be tested individually through its own interface.
If we use the switch statement to analyze the client in order to determine which algorithm to use, we can use the policy mode to hide the process of this judgment in the background and implement each algorithm with a strategy class. This simplifies the client's code and hides the implementation details.
#ifndef strategy_h#define strategy_h#include<iostream>using namespace Std;class cashsuper{friend class Cashcontext;protected:virtual Double Acceptcash (double money) = 0;};/ * Factory mode class Cashcontext{cashsuper *cs;public:cashcontext (Cashsuper *CASHS): cs (CASHS) {}void operator = (Cashsuper * re) { cs = re;} Double GetResult (double money) {return Cs->acceptcash (money);}}; */class cashcontext{cashsuper *cs;char Strategytype;public:cashcontext () {}cashcontext (char &SType); void operator = (char &stype);d ouble getresult (double money) {if (cs = = NULL) {cout << ' the strategy is Unfind. '; retur n-1; }return Cs->acceptcash (Money);}}; Class Cashnormal:public Cashsuper{//friend class Cashcontext;public:double Acceptcash (double money) {return money;}}; Class Cashrebate:public Cashsuper{//friend class Cashcontext;double rebate;public:cashrebate (double discount): Rebate (Discount) {}double Acceptcash (double money) {return money*rebate;}}; Class Cashreturn:p ublic cashsuper{//friend class CashconText;double Basecash,returncash;public:cashreturn (Double basec,double RETURNC): Basecash (BaseC), ReturnCash (ReturnC {}double Acceptcash (double money) {If [Money >= Basecash] return money-returncash;return money;}}; Cashcontext::cashcontext (char &stype): Strategytype (stype) {switch (strategytype) {case (' a '): Case (' a '): cs = new Cashnormal;break;case (' B '): Case (' B '): cs = new Cashrebate (0.8); Break;case (' C '): Case (' C '): cs = new Cashreturn (1000, Break;default:cs = Null;break;}} void Cashcontext::operator = (char &stype) {strategytype = Stype;switch (strategytype) {case (' a '): Case (' a '): cs = new Cashnormal;break;case (' B '): Case (' B '): cs = new Cashrebate (0.8); Break;case (' C '): Case (' C '): cs = new Cashreturn (1000, Break;default:cs = Null;break;}} #endif
#include "strategy.h" int main () {double Shouldpay = 1520;/* The implementation of the simple Factory mode, The details of the following three strategies are exposed to the client Cashnormal Cashn; Cashrebate CASHRB (0.95); Cashreturn Cashrt (1000, 200); Cashcontext CASHC (&cashn) cout << "The real pay is:" << cashc.getresult (shouldpay) << endl;cashc=& Amp;cashrb;cout << "The real pay is:" << cashc.getresult (shouldpay) << endl;cashc=&cashrt;cout < ;< "The Real pay is:" << cashc.getresult (Shouldpay) << endl;*///implementation is implemented in the policy mode, whose payment details are not visible on the client, If you want to add or remove a policy//we can add the policy class in Strategy.h, and then add this class to the Cashcontext implementation, so that the client//seldom modify the information, implement the details of the hidden cout << "Please enter the Strategy type, ' a ' or ' a ' instand the Normalcash, \ n "<<" ' B ' or ' B ' instand the Rebatecash, ' C ' or ' C ' instand the RE turncash.\n "; char type; Cashcontext cashc;while (cin >> type) {CASHC = Type;cout << "The real pay is:" << Cashc.getresult (Shoul Dpay) << Endl;} return 0;}
If you want to reprint, please indicate the source.
Design pattern C + + Implementation II: Policy mode