The strategy pattern is a method of defining a series of algorithms, conceptually, all of which are done the same work, but the implementation is different, it can call all the algorithms in the same way, reducing the coupling between the various algorithm classes and the use of the algorithm class.
The strategy (Coperate) class layer of the policy mode defines a number of algorithms or behaviors that can be reused for the context, and inheritance helps to extract the father-in-law features of these algorithms.
The policy model simplifies unit testing because each algorithm has its own class that can be tested individually through its own interface.
Each algorithm guarantees that it has no errors, and modifying one of them does not affect other algorithms.
The test code is as follows:
stra_fact.h//strategy base Classes Class Coperation{public:int Number_a;int number_b;public:virtual double GetResult ();};/ /Policy Specific Class-addition class class Addoperation:public coperation{public://constructor addoperation (int num_a,int num_b); virtual double GetResult ();};/ /Policy Specific class-subtraction class class Suboperation:public coperation{public://constructor suboperation (int num_a,int num_b); virtual double GetResult ();};/ /Policy specific class-The remainder class class Remoperation:public coperation{public://constructor remoperation (int num_a,int num_b); virtual double GetResult ();};/ /context, using a base class to configure Class context{private:coperation* Op;public: context (int num_a, int num_b, char sign);d ouble GetResult ();};
Stra_fact.cpp#include "stra_fact.h"//Strategy base class method implementation double Coperation::getresult () {Double result = 0;return result;} Policy addition class--constructor addoperation::addoperation (int num_a,int num_b) {number_a = Num_a;number_b = Num_b;} Policy addition class--The parent class method overrides double Addoperation::getresult () {return number_a + Number_b;} Policy subtraction Class--constructor suboperation::suboperation (int num_a,int num_b) {number_a = Num_a;number_b = Num_b;} Policy Subtraction Class--The parent class method overrides double Suboperation::getresult () {return number_a-number_b;} Policy redundancy Class-constructor remoperation::remoperation (int num_a,int num_b) {number_a = Num_a;number_b = Num_b;} Policy redundancy class--The parent class method overrides double Remoperation::getresult () {return number_a% Number_b;} Context, using a base class to configure Context::context (int num_a, int num_b, char sign) {op = new coperation; Switch (sign) {case ' + ': {addoperation * temp = new addoperation (num_a,num_b); op = temp;break;} Case '-': {suboperation * temp = new suboperation (num_a,num_b); op = temp;break;} Case '% ': {remoperation * temp = new remoperation (num_a,num_b); op = Temp;break;}}} Double Context::getresult () {return Op->getresult ();}
User.h#include "iostream" #include "stra_fact.h" using namespace std;//client int main () { int num_a, num_b; char sign; cout<< "Put your first number:"; cin>>num_a;cout<< "Put your second number:";cin>>num_b;cout<< "Put your sign:"; cin>>sign; Context * Context = new context (Num_a, Num_b, sign); Cout<<context->getresult () <<endl; return 0;}
<c/c++ Version > Design mode learning strategy Mode + Factory mode