Strategy Mode of design pattern (C ++ implementation)

Source: Internet
Author: User

The rule mode is a commonly used design mode. It is mainly embodied in the fact that objects can have certain behaviors, but different implementation methods are used in different scenarios!

Similar situations are often encountered in software development. There are multiple algorithms or policies for implementing a function. We can select different algorithms or policies based on different environments or conditions to complete this function. Such as search and sorting. A common method is hard coding in a class. To provide multiple search algorithms, you can write these algorithms into a class, multiple methods are provided in this class. Each method corresponds to a specific search algorithm. Of course, these search algorithms can also be encapsulated in a unified method, through if... Else... Or case and other condition judgment statements for selection. Both of these methods can be called hard encoding. To add a new search algorithm, you need to modify the source code of the encapsulated algorithm class. Replace the search algorithm, you also need to modify the client call code. This algorithm class encapsulates a large number of search algorithms,

This type of code is complex and difficult to maintain. If we include these policies on the client, this approach is not desirable, it will lead to a large client program and difficult to maintain, if there are a large number of available algorithms, the problem will become more serious.

What we need to do is encapsulate each method in different classes and call corresponding methods as needed!

 

Here we also use two examples to illustrate the problem:

1: how to implement the standard rule mode!

2: guide the story of the Savior Zhao yundong Wu in the classic Zhuge Liang strategy!

 

First, let's look at the first one. It's easy to understand and shows the general structure of the Policy mode!

Class strategyinterface // The abstract interface {public: Virtual void execute () = 0 ;}; class concretestrategya: Public strategyinterface // method A implements the interface, which is the same as {public: virtual void execute () {cout <"called concretestrategya execute method" <Endl ;}}; class concretestrategyb: Public strategyinterface {public: Virtual void execute () {cout <"called concretestrategyb execute method" <Endl ;}}; class concretestrateg YC: Public strategyinterface {public: Virtual void execute () {cout <"called concretestrategyc execute method" <Endl ;}}; class context {// main operation class private: strategyinterface * _ strategy; public: Context (strategyinterface * Strategy): _ strategy (Strategy) {} void set_strategy (strategyinterface * Strategy) {_ strategy = strategy;} void execute () {_ strategy-> execute () ;}}; int main (INT argc, char * AR GV []) {concretestrategya; concretestrategyb; Context contexta (& concretestrategya); Context contextb (); // calls the corresponding method using polymorphism in the function! Contextb.exe cute (); contextc.exe cute (); contexta. set_strategy (& concretestrategyb); contexta.exe cute (); contexta. set_strategy (& Strategy); contexta.exe cute (); Return 0 ;}

The above example is enough to understand the general implementation method of the Policy mode. Next, let's look at the second question.

First of all, there are three tips in this scenario. Zhao Yun is the performer who opens the tips method, so the main function should be shown in the UML diagram below.

 

 

Next let's take a look at how to use C ++ to implement this scenario:

Class istrategy {public: Virtual void operator () = 0 ;}; class fun1: Public istrategy {void operator () {cout <"Ask Joe for help, let Sun Quan not kill Liu Bei "<Endl ;}; class fun2: Public istrategy {void operator () {cout <" ask Wu Guotai to give a green light, allow "<Endl ;}}; class fun3: Public istrategy {void operator () {cout <" ask Mrs sun to disband the troops, escape time "<Endl ;}}; class contualclass {PRIVATE: istrategy * m_psrategy; public: contualclass () {} contualclass (istrategy * pirevt) {This-> m_psrategy = Pirevt;} void operator () {m_psrategy-> operator () ;}}; int main () {// remove the first cout when I arrived at Wu Guo <("----------- remove the first cout when I arrived at Wu Guo -----------") <Endl; contualclass * context = new contualclass (New fun1 (); // get the MasterCard context-> operator (); // disassemble and execute it. // Liu Bei is happy, split the second cout <("----------- Liu Bei is happy, and split the second one -------------") <Endl; Context = new contualclass (New fun2 ()); context-> operator (); // The second tip is executed. // Sun Quan's soldier is chasing it. What should I do? Remove the third cout. <("----------- What should I do if Sun Quan's soldier is chasing? Remove the third ") <Endl; Context = new contualclass (New fun3 (); Context-> operator (); // remove Mrs sun from the system (" pause "); return 0 ;}

This example is very intuitive. This is often used to explain the policy mode! It's time for dinner. It's just a flash.

 

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.