First, UML diagram
Ii. Overview
Policy mode: He defined the algorithm family, respectively encapsulated, so that they can replace each other, this mode allows the algorithm changes, will not affect the use of the algorithm of the customer.
Third, the advantages
(1) The strategy pattern is a method to define a series of algorithms, conceptually, all of these algorithms do the same work, but the implementation is different, he can call all the algorithms in the same way, reducing the various algorithm classes and the use of the coupling between the algorithm classes.
(2) The strategy class of the policy pattern was once a reusable algorithm or behavior that defines some columns for the context. Integration helps to extract public functionality from these algorithms.
(3) The strategy model simplifies unit testing because each algorithm has its own class that can be tested individually through its own interface.
(4) The strategy pattern is used to encapsulate the algorithm.
(5) As long as you hear the need to apply different business rules at different times during the analysis process, you can consider the possibility of using a policy model to handle this change.
(6) The Simple Factory mode requires the client to recognize two classes, and the use of the policy mode and the simple factory pattern, the client only need to know a class context.
Iv. implementation of C + +
(1) Strategy: Strategy.h
#ifndef strategy_h#define strategy_h#include <string> #include <math.h>//Parent abstract class class Cashsuper{public: Virtual Double Acceptcash (double money) =0;};/ /subclass: Normal pay type class Cashnormal:public cashsuper{public:double Acceptcash (double money) {return money;}};/ /Subclass: Rebate Type class Cashreturn:public cashsuper{private:double moneycondition;double Moneyreturn;public:cashreturn ( Double moneycondition,double Moneyreturn) {this->moneycondition=moneycondition;this->moneyreturn= Moneyreturn;} Double Acceptcash (double money) {double result=money;if (money>moneycondition) Result=money-floor (money/ moneycondition) *moneyreturn;return result;}};/ /Subclass: Discount Type class Cashrebate:public cashsuper{private:double moneyrebate;public:cashrebate (double moneyrebate) {this- >moneyrebate=moneyrebate;} Double Acceptcash (double money) {return money*moneyrebate;}}; #endif Strategy_h
(2) Strategy: CashContext.h
#ifndef cashcontext_h#define cashcontext_h#include "Strategy.h"//Strategy 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) Client: Main.h
#include "CashContext.h" #include <iostream> #include <stdlib.h>void main () {double total=0;double totalprices=0;//normal charge cashcontext* cc1=new cashcontext (1); totalprices=cc1->getresult; total+=totalPrices; std::cout<< "Type: normal charge totalprices:" <<totalPrices<< "total :" <<TOTAL<<STD: : endl;totalprices=0;//return type cashcontext* cc2=new cashcontext (2); Totalprices=cc2->getresult (+); total+= totalprices;std::cout<< "Type: Full 300 totalprices:" <<totalPrices<< "total :" << total<<std::endl;totalprices=0;//discount Type cashcontext* cc3=new cashcontext (3); Totalprices=cc3->getresult (300 );total+=totalprices;std::cout<< "Type: Hit 80 percent totalprices:" <<totalPrices<< "total :" < <total<<std::endl;totalprices=0;system ("Pause");}
Big Talk Design pattern C + + Implementation-2nd Chapter-Policy mode