Design pattern Implementation C + +--Policy mode strategy (object behavior type)

Source: Internet
Author: User

1. Questions

  Travel: We can have several strategies to consider: Can ride bicycles, cars, do trains, airplanes. Each policy can get the same results, but they use different resources. Select the basis for the policy

Is the cost, the time, the use of tools and the convenience of each method.

2. Solution

  Policy mode: Define a series of algorithms, encapsulate each algorithm, and make them interchangeable with each other. This mode allows the algorithm to be independent of the customers who use it.

The strategy pattern distinguishes between the object itself and the arithmetic rules, and its function is very powerful, because the core idea of the design pattern itself is the idea of the polymorphism of object-oriented programming.

Strategy Pattern Class Diagram:

        

3. Application Scenarios

  1, multiple classes only differ in performance behavior, you can use the Strategy mode, at run time to dynamically select the specific behavior to execute.

2. Different strategies (algorithms) need to be used in different situations, or strategies may be implemented in other ways in the future.

3, the customer hides the specific strategy (algorithm) Implementation details, each other completely independent.

4. Composition

Environment Class (context): Configured with a Concretestrategy object. Maintains a reference to the Strategy object. You can define an interface to let strategy access its data.

Abstract policy Class (strategy): Defines the public interface for all supported algorithms. The context uses this interface to invoke an algorithm defined by a concretestrategy.

Specific policy class (Concretestrategy): Implement a specific algorithm with strategy interface.

5. Pros and cons

  Advantages:

1. The strategy mode provides a way to manage related algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors.  Proper use of inheritance can transfer common code to the parent class, thus avoiding duplicate code. 2. The policy mode 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 different 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, so that it is impossible to 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 the action, all in a multiple transfer statement, more primitive and backward than the method of using inheritance. Cons: 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 pattern applies only to situations where the client knows all the algorithms or behaviors. 2, the policy mode causes a lot of policy classes, each specific policy class will produce a new class. 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 use the enjoy meta mode to reduce the number of objects. 6. Implement
1#include <iostream>2 using namespacestd;3 4 classTravelstrategy5 {6  Public:7         Virtual voidTravel () =0;8 };9 Ten classAirplanstrategy: PublicTravelstrategy One { A  Public: -         voidTravel () -         { thecout <<"Travel by Air Plan"<<Endl; -         } - }; -  + classTrainstrategy: PublicTravelstrategy - { +  Public: A         voidTravel () at         { -cout <<"Travel by train"<<Endl; -         } - }; -  - classBusstrategy: PublicTravelstrategy in { -  Public: to         voidTravel () +         { -cout <<"Travel by bus"<<Endl; the         } *};enumTravel {Airplan, TRAIN, BUS}; $ Panax Notoginseng classpersoncontext{ -  Public: thePersoncontext (enumTravel TR) +         { A                 if(tr = =Airplan) theStrategy =Newairplanstrategy (); +                 Else if(tr = =TRAIN) -Strategy =Newtrainstrategy (); $                 Else if(tr = =BUS) $Strategy =Newbusstrategy (); -                 Else -Strategy =NULL; the         } - Wuyi         voidTravel () the         { -Strategy->Travel (); Wu         } -  About Private: $travelstrategy*strategy; -};
View Code

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.