Deep analysis of the application of the strategy pattern in the design pattern in C + + programming _c language

Source: Internet
Author: User
Tags inheritance

The policy pattern is also a very common design pattern and is not complex. Let's take a look at the pattern here.
Definition: A policy pattern defines a series of algorithms, encapsulates each algorithm, and makes them interchangeable. The policy pattern allows the algorithm to vary independently from the customer who uses it.

Role:

    • Abstract policy Role (strategy): Abstract Policy class.
    • Specific policy role (CONCRETESTRATEGY): Encapsulates the algorithms and behaviors that continue to be relevant.
    • Environment Role (context): holds a reference to a policy class that is eventually invoked to the client.

UML diagram:

Example:

#include <iostream> using namespace std; 
Class Weaponbehavior {public:void virtual useweapon () = 0; 
 
}; Class Ak47:public Weaponbehavior {public:void useweapon () {cout << ' use AK47 to shoot! ' << end 
  L 
 
} 
}; Class Knife:public Weaponbehavior {public:void useweapon () {cout << "use knife to kill!" << en 
  dl 
 
} 
}; 
  Class Character {Public:character () {weapon = 0; 
  } void Setweapon (Weaponbehavior *w) {this->weapon = W; 
void virtual fight () = 0; 
Protected:weaponbehavior *weapon; 
 
}; 
    Class King:public Character {public:void fight () {cout << "the King:"; if (This->weapon = NULL) {cout << "you don ' t have a weapon! 
    Please Set weapon! "<< Endl; 
    else {weapon->useweapon (); 
} 
  } 
}; 
  int main () {Weaponbehavior *ak47 = new AK47 (); Weaponbehavior *knife = new Knife ();    
 
  Character *kin = new King ();   
  Kin->fight ();  
 
  cout << Endl; 
  Kin->setweapon (AK47); 
  Kin->fight (); 
 
  cout << Endl; 
  Kin->setweapon (knife); 
 
  Kin->fight (); 
return 0; 
 }

Applicability:
1, multiple classes differ only in performance behavior, and you can use the strategy mode to dynamically select the behavior to perform at runtime.
2, you need to use different policies (algorithms) in different situations, or the strategy may be implemented in other ways in the future.
3, the customer hides the specific strategy (algorithm) Implementation details, each other completely independent.

Disadvantages
Advantages:
1, the strategy model provides a way to manage the family of related algorithms. The hierarchical structure of a policy class defines an algorithm or a behavior family. Proper use of inheritance can move common code into the parent class, thereby avoiding code duplication.
2, using a policy pattern avoids the use of multiple criteria (IF-ELSE) statements. Multiple conditional statements are not easy to maintain, and it mixes the logic of the algorithm or behavior with the logic of the algorithm or behavior, all in a multiple conditional statement, which is more primitive and backward than the method of using inheritance.
Disadvantages:
1, the client must know all the policy classes and decide which policy class to use. This means that the client must understand the difference between these algorithms in order to select the appropriate algorithm class at the right time. In other words, the policy pattern applies only to situations where the client knows the algorithm or behavior.
2, because the policy pattern encapsulates each specific policy implementation individually as a class, the number of objects can be significant if alternative strategies are many.

Differences from other design patterns:
1, with State mode
In solving the problem, the state mode is to solve the internal state of change, and the strategy mode is to solve the change of the interior algorithm. In the solution, the state mode is the change of self-control state, and the strategy mode is formulated by the external use of what strategy.
2, Simple factory model
A simple factory pattern is a creation pattern that focuses on the creation of objects. The policy pattern is the behavior pattern, which concerns the encapsulation of behavior. The Simple factory pattern returns a suitable class for you to use based on different conditions, and then the caller uses the class returned by the factory class to do the appropriate action. The policy pattern is that you must first create an instance of the class that you want to use, and then the instance is passed as a parameter, and then the unused algorithm is invoked through that instance. In the simple factory model, we can select a class to instantiate the object through the condition, and the policy mode will select the work of the corresponding object to the user of the mode, and it does not do the selection work itself.

Related Article

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.