Strategy Design Pattern

Hello everyone, friends. My name is Alex and I am a professional software developer and creator in the web industry. I have been studying languages ​​for many years, sharing my experience with others.





Today I want to talk to you about the Strategy design pattern . I will try to convey to you the principles and essence of the template without water, and show you how to apply it in practice.





What's the point?

Design patter Strategy  or design pattern Strategy  refers to behavioral design patterns. Its task is to identify similar algorithms that solve a specific problem. The implementation of the algorithms is placed in separate classes and the ability to select algorithms is provided at runtime.





The template makes it possible in the process of execution to choose a strategy (algorithm, tool, approach) for solving the problem. 





What is the problem?

Let us consider the tasks in the solution of which this approach can be applied. 





Imagine that you are faced with the task of writing a web portal for real estate search. MVP ( Minimum Viable Product ) or minimum working product has been designed and prioritized by your team of Product Managers and the portal should have functionality for apartment buyers. That is, the target users of your product are, first of all, those who are looking for a new home to buy. One of the most requested features should be the ability to:





  • Select the area on the map where the buyer wishes to purchase a home





  • And indicate the price range of prices for apartments for filtering.





, . , , , .





Product Manager' , , . - . , , . 





, - , .





. , . - - , , . 





, . . , , . , , , , .





, , . , , , , , . : 





  • -





  • -





  • , ,





  • ,





  • , , .





Super class with a unified algorithm implementation method.
- .

?

















  • - , , .. 





- .   , . , , , . , , - - . -. 





, - . - -. 





Class diagram of the Strategy pattern
Strategy

, -, . .





-, , doSearch(filters)



, , . 





, - , (, , , , ..), , , - -, , . , , backend, .





- - , . -  - , . , getData



, filters



. , -  setStrategy



, . , , , . 





, GOlang. - doSearch







Strategy.go
Strategy.go

, . , . Map



.





. struct



, Strategy



. , , , . :





FirstAlgorithm.go
FirstAlgorithm.go
SecondAlgorithm.go
SecondAlgorithm.go

. - . ? - , :





  • struct







  • initStrategy



    , -





  • struct setStrategy



    ,





  • getData



    , .





Context.go
Context.go

- . -, . getData



. , - ( - ) , . , , . :





Client.go
Client.go

:





First implements strategy map[role:1]







Second implements strategy map[role:2]







, . , , . .





- . , . PHP.





?

Strategy?





  1. . -, -. 





  2. - . -, -. 





  3. . . 





  4. . , -, .





Friends, we got acquainted with the behavioral design pattern Strategy . The template is used to highlight similar algorithms that solve a specific problem. We looked at the implementation in the GOlang language with you , got acquainted with the possibilities of the approach and figured out when it is better to use it.





I was glad to talk to you, Alex Versus . Good luck!








All Articles