<c/c++ Version > Design mode learning strategy mode

Source: Internet
Author: User

Policy mode (strategy): It defines 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 users.

You should consider using the policy mode in the following scenarios:

1. If there are many classes in a system, the difference between them is only their behavior, then the use of a policy model can dynamically let an object in many behaviors to choose a behavior.

2. A system needs to dynamically select one of several algorithms. Then these algorithms can be packaged into a specific algorithm class, and these specific algorithm classes are a subclass of an abstract algorithm class. In other words, these specific algorithm classes are

There is a unified interface, due to the polymorphism principle, the client can choose to use any of the specific algorithm classes, and only hold a data type object that is an abstract algorithm class.

3. The data used by a system's algorithms cannot be made known to the client. The policy pattern avoids having the client involve complex and algorithmic-related data that is not necessarily exposed.

4. If an object has a lot of behavior, if the appropriate pattern is not used, the behavior will have to use multiple conditional selection statements to implement. At this point, using the policy mode, transfer these behaviors to the corresponding specific policy class, you can

To avoid using multiple conditional selection statements that are difficult to maintain and to embody the concept of object-oriented design.

Advantages and disadvantages of the policy model

The strategy model has many advantages and disadvantages. It has the following advantages:

1. The policy model provides a way to manage the associated algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors. Proper use of inheritance can move common code into the parent class, thus avoiding duplicate code.

2. The policy model provides a way to replace the inheritance relationship. Inheritance can handle multiple algorithms or behaviors. If the policy mode is not used, then the environment class using the algorithm or behavior may have some subclasses, each of which provides a

The same algorithm or behavior. However, the user of the algorithm or behavior is mixed with the algorithm or the behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, thus not

May evolve independently. Inheritance makes it impossible to dynamically change an algorithm or behavior.

3. Use the policy mode to avoid using multiple conditional transfer statements. Multiple transfer statements are difficult to maintain, and it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or action, all listed in a multiple-turn

It is more primitive and backward to move statements than to use inherited methods.

The disadvantages of the strategy model are:

1. The client must know all of the policy classes and decide for itself 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 mode applies only to the client

Knowing all the algorithms or behaviors of the situation.

2. The policy model causes a lot of policy classes. It is sometimes possible to design a policy class to be shareable by saving the environment-dependent state to the client, so that the policy class instance can be used by different clients. In other words, you can make

Use the enjoy meta mode to reduce the number of objects.

The strategy model has a wide range of relationships with many other models. Strategy is easily confused with bridge mode. Although they are similar in structure, they are designed to solve different problems. Strategy mode focuses on algorithms

Encapsulation, while bridge mode focuses on separating abstractions and implementations, providing different implementations for an abstract system. Bridge mode and strategy model are very good embodiment of "Favor composite over inheritance" point of view.

strategy.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, use a base class to configure Class context{private:coperation* Op;public:    context (coperation* temp);d ouble getresult ();};

Strategy.cpp#include "Strategy.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::context (coperation* temp) {    op = temp;} Double Context::getresult () {return Op->getresult ();}

User.h#include "iostream" #include "Strategy.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;    Switch (sign)    {case    ' + ': {Context *context = new context (new Addoperation (Num_a,num_b)); cout<<context- >getresult () <<endl;break;} Case '-': {Context *context = new Context (new Suboperation (Num_a,num_b)); Cout<<context->getresult () << Endl;break;} Case '% ': {context *context = new context (new Remoperation (Num_a,num_b)); Cout<<context->getresult () << Endl;break;}     }     return 0;}

Strategy Blur quiz: http://biancheng.dnbcw.info/c/169125.html


<c/c++ Version > Design mode learning strategy mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.