Concept:
The policy pattern defines a series of algorithms that respectively encapsulated, Let they are between can be replaced with each other. This pattern allows the algorithm to change without affecting the customer using the algorithm . a strategy, in essence, refers to an algorithm.
Example:
A vivid and simple example always makes it easy to understand obscure concepts. Let's take a look at a strategic model of car prices.
we know that the brand and quality of the car determines its price. Like BMW, Ferrari (Ferrali) and Mercedes-Benz (Benz) three cars, their prices are certainly not the same. If you want to know the price of it, you can ask sales people and so on. But in the computer, we can not directly ask sales staff Ah!
For them, although the respective price is different, it is a different algorithm, but the acquisition of the price is a public operation (the policy mode is required to encapsulate the change of the algorithm). Thus, you can create an abstract parent class car class(car) that declares an interface to get the price virtual GetPrice function. Then create three sub-classes: Bmw,ferrali and Benz, respectively, inherited from car. By inheriting relationships, each subclass can overwrite the parent class's GetPrice function and then get the subclass car price in the client by invoking a different car subclass algorithm.
To summarize:
1, we regard the parent car as an abstract strategy class , providing a getprice interface to get the price;
2, each kind contains the price getprice car class for the specific strategy class (the car price of each brand is inconsistent, resulting in different calculation algorithm), to rewrite the parent class car GetPrice function;
3. Establish an environment context class Pricecontext, maintain a reference to the parent class Car Object (the pointer object can be polymorphic), and finally call the specific policy class object to the client.
UML diagram:
Code:
#include <iostream>using namespace Std;class car{public:float m_fprice;public:virtual float GetPrice () {return m_ fprice*1;}}; Class Bmw:public Car{public:float GetPrice () {return m_fprice*3;}}; Class Ferrali:public Car{public:float GetPrice () {return m_fprice*11;}}; Class Benz:public Car{public:float GetPrice () {return m_fprice*6;}}; Class Pricecontext{public:int m_iflag;private:car* M_ccar;public:pricecontext (car* ccar): M_cCar (cCar) {//this->m _ccar = Ccar;} Float Getpricecontext () {m_ccar->m_fprice = 10000;return (M_ccar->getprice ());}}; int main () {float fprice=0.0;int itag=0; cout<< "----policy mode start----" <<endl; cout<< "Bmw:1,ferrali:2,benz : 3 "<<endl; cout<< "Please enter the price of the car you wish to inquire about:"; cin>>itag; pricecontext* Pricecontext;switch (iTag) {case 1:pricecontext = new Pricecontext (new BMW); Break;case 2:pricecontext = new Pricecontext (new Ferrali); Break;case 3:pricecontext = new Pricecontext (new Benz); break;default:pricecontext = new Pricecontext (new Car); Fprice = prIcecontext->getpricecontext ();d elete pricecontext;pricecontext = NULL; cout<< "Price is:" <<fPrice<<endl; cout<< "----policy mode end----" <<endl;return 1;}
Strategy mode combined with simple Factory mode :
In the simple factory model, the dynamic creation of objects is placed in the factory class. and the basic policy mode client is to do the algorithm judgment and object creation. Therefore, we can imitate the simple factory, move the judgment of the algorithm to the environment context class, minimize the client's responsibility and reduce the coupling. In the relatively simple Factory mode, the client does not appear in the same base class, more to hide the specific implementation details of the algorithm.
Code:
Class Pricecontext{public:int m_iflag;private:car* m_ccar;public:pricecontext (int iflag) //construct no longer wear objects, pass identity {switch (Iflag) {Case 1:m_ccar = new Bmw;break;case 2:m_ccar = new Ferrali;break;case 3:m_ccar = new Benz;break;default:m_ccar = new Car;b Reak;}} Float Getpricecontext () {m_ccar->m_fprice = 10000;return (M_ccar->getprice ());}}; int main () {float fprice=0.0;int itag=0; cout<< "----policy mode start----" <<endl; cout<< "Bmw:1,ferrali:2,benz : 3 "<<endl; cout<< "Please enter the price of the car you wish to inquire about:"; cin>>itag; pricecontext* pricecontext = new Pricecontext (ITAG); fprice = Pricecontext->getpricecontext ();d elete PriceContext; Pricecontext = NULL; cout<< "Price is:" <<fPrice<<endl; cout<< "----policy mode end----" <<endl;return 1;}
Summary:
1, the strategy model defines a series of algorithms, conceptually, all the algorithms are done the same operation, but the performance of different behavior. By invoking all the algorithms in the same way (using polymorphism), the coupling between the algorithm classes and the use of the algorithm classes is reduced;
2, The customer hides the specific strategy ( algorithm ) Implementation details, each other completely independent ;
3, the Strategy mode simplifies the unit test, each algorithm has its own class, can be tested by its own interface alone;
4. the client must know all the policy classes and decide for themselves which policy class to use. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows all the algorithms or behaviors .
Design patterns that's something--strategy mode