This is an introduction GoF In design pattern Strategy pattern

<>Strategy

* The first result after the division of modern software specialty is “ Framework and Application Division ”,“ Component collaboration ” Patterns through late binding , To realize the loose coupling between framework and application , Is a common mode of cooperation between the two
*
In the process of software construction , Some objects may use a variety of algorithms , Constantly changing , If you code all these algorithms into the object , Will make the object extremely complex ; And sometimes supporting unused algorithms is a performance burden
GoF Design pattern definition

* Define a series of algorithms , Package them one by one , And make them interchangeable ( change ), This pattern makes the algorithm independent of the client program that uses it ( stable ) And change ( extend , Subclassification )
Structure

Code
#include <iostream> using namespace std; enum { method1, method2, method3,
method4}; class Test { public: /* not used Strategy Mode time , If there is a new feature, you need to run In function changes , The reusability of code is poor
*/ void run(int methodNum) { if (methodNum == method1) { cout << "method1" <<
endl; } else if (methodNum == method2) { cout << "method2" << endl; } else if (
methodNum== method3) { cout << "method3" << endl; } else if (methodNum ==
method4) { cout << "method4" << endl; } } }; int main() { Test().run(0); system(
"pause"); return 0; } #include <iostream> using namespace std; class IMethod {
public: virtual void method() = 0; virtual ~IMethod() { } }; class Factory {
public: virtual IMethod* create() = 0; virtual ~Factory() { } }; class Method1 :
public IMethod { public: virtual void method() { cout << "method1" << endl; } };
class Method1Factory : public Factory { public: virtual IMethod* create() {
return new Method1; } }; class Method2 : public IMethod { public: virtual void
method() { cout << "method2" << endl; } }; class Method2Factory : public Factory
{ public: virtual IMethod* create() { return new Method2; } }; /* use Strategy
Mode time , If there are new features run The function does not need to be changed , Code reusability is improved */ class Test { public: Test(Factory* factory)
{ iMethod = factory->create(); } void run() { iMethod->method(); } ~Test() {
delete iMethod; } private: IMethod* iMethod; }; int main() { Factory* factory =
new Method1Factory; Test(factory).run(); delete factory; system("pause"); return
0; }
summary

* Strategy And its subclasses provide a series of reusable algorithms for components , Thus, the type can be easily switched between algorithms at runtime
* Strategy Patterns provide an alternative to conditional judgment statements , Eliminate conditional judgment statements , It's decoupling . Code with many conditional statements is usually required Strategy pattern
* If Strategy Object has no instance variables , Then each context can share the same Strategy object , This saves object overhead
In case of infringement , Please contact to delete , If there is a mistake , Welcome to correct , thank you

Technology
©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )