C + + implementation of the strategy mode of design pattern

Source: Internet
Author: User

Policy mode (strategy): Defines the algorithm family, respectively encapsulated, so that they can replace each other, this pattern lets the algorithm change, does not affect the use of the algorithm of the customer.

Advantages:

1, simplified unit testing, because each algorithm has its own class, can be tested by their own interface alone.
2, avoid the use of multiple conditional transfer statements in the program, make the system more flexible, and easy to expand.
3, so the algorithm is done by the same work, but the implementation of different, it can call all the algorithm in the same way, reducing the various algorithms and the use of the coupling between the algorithm class.


Disadvantages:
1, because each specific policy class will produce a new class, it will increase the number of classes that the system needs to maintain.
2, in the basic strategy mode, select the specific implementation of the responsibilities used by the client object, and transferred to the policy mode of the context object.


Because in the basic policy mode, the responsibility for selecting the specific implementation is assumed by the client object and forwarded to the policy mode context object. This itself does not touch the client needs to choose the pressure of judgment, and the strategy model and the simple engineering model, the choice of the specific implementation of the responsibilities can also be undertaken by the context, which will minimize the client's responsibilities.


Strategy.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace std;/************************************* policy base class ****** /class strategybase//mainly defines the virtual function {public:virtual void Multiway_tour () =0;//description is a pure virtual function ( Virtual functions that are not implemented) must be so declared};/************************************* specific policy classes ****************************************/class Strategyfirstchild:public strategybase//Policy Subclass, which implements the virtual method defined by the parent class {public:void multiway_tour () {cout<< "I ll Go Tourism on Feet "<<endl;}}; Class Strategysecondchild:public strategybase//policy subclass, which specifically implements the virtual method defined by the parent class {public:void multiway_tour () {cout<< " I ' ll go tourism by train "<<endl;}};/ Scheduling class ****************************************/class Context//scheduling class, according to the parameters passed in, Select a specific policy----for optimization < reference tutorial >{private:strategybase *strategychild;public:context (strategybase *child) { Strategychild=child;} void Multiway_tour () {strategychild->multiway_tour ();}};/ Client ********/int Main () {cout<< "test program" <<endl;//"Specific policy class" use context only when defining multiple scheduling classes * Context_a=new Context (New Strategyfirstchild ()); Context*context_b=new Context (new Strategysecondchild ();, Context*context_c=new context (new Strategysecondchild ()) ;//Call method, only through "scheduling class" Implementation, the difference between the algorithm has been masked context_a->multiway_tour (); Context_b->multiway_tour (); Context_c->multiway_tour ();
<span style= "White-space:pre" ></span>delete context_a;
<span style= "White-space:pre" ></span>delete context_b;
<span style= "White-space:pre" ></span>delete context_c;
Cout<<endl;system ("pause"); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + implementation of the strategy mode of design pattern

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.