Strategy (Strategic model)

Strategy is a behavioral pattern.

Intent: Define a set of algorithms, encapsulate them one by one, and make them interchangeable. This pattern allows the algorithm to change independently of the customers using it.

Strategy is a graphic expression, the so-called strategy is the plan, we all know that everything has a variety of plans, and different plans can solve the problem, so these plans can be replaced with each other. We abstract the solution from the problem so that we can optimize the solution independently of the problem, which is the core idea of the strategy pattern.

For example,

If you don’t understand the above description, it doesn’t matter. Design patterns need to be used in daily work. Examples will help you understand better.

Map navigation

We can walk, ride a bike, drive a car or take a bus to go anywhere. Different schemes can help us reach the destination. Therefore, it is obvious that we should turn these schemes into strategies and encapsulate them to receive the starting point and destination and output the route.

layout

For example, if we make a report system, we use the grid layout in THE PC and the streaming layout in the mobile terminal. In fact, the content is still the same, but the layout will do different adaptation with different terminal sizes, so the adaptation of the layout is a strategy, which can have nothing to do with the report content.

The advantage of decoupling the layout strategy from the code is that it can be isolated and later adapted to different size scenarios, such as TV sets, projectors, and so on, without making any changes to the rest of the code.

Sorting algorithm

What sort algorithm is used when we call.sort? Could it be bubbling, fast, insert sort? In fact, no matter what kind of sorting algorithm, essentially does the same thing, we can pre-wrap the sorting algorithm, for different properties of the array to call different sorting algorithm.

Intention to explain

Intent: Define a set of algorithms, encapsulate them one by one, and make them interchangeable. This pattern allows the algorithm to change independently of the customers using it.

Algorithm can be understood as a strategy, we have formulated many solution strategy, a scene of these strategies can be independent to solve the problem in this scenario, so the next time meet with this scenario, we can choose any strategy to solve, and we can also from the scene, optimization strategy alone, as long as the interface stays the same.

The intent is essentially to decouple, and then you can divide. Think of a complex system. If all policies are coupled to business logic, only those who understand the business can carefully maintain them, but if the policies are decoupled from the business, we can maintain these policies independently, bringing more flexible changes to the business.

chart

  • Strategy: indicates the policy public interface.
  • ConcreteStrategy: ConcreteStrategy that implements this interface.

As long as your policy conforms to the interface, it satisfies the conditions of the policy pattern.

The code example

The following example is written in typescript.

interface Strategy {
  doSomething: () = > void
}

class Strategy1 implements Strategy {
  doSomething: () = > {
    console.log('Implementation 1')}}class Strategy2 implements Strategy {
  doSomething: () = > {
    console.log('Implementation 2')}}/ / use
new System(new Strategy1()) // The system implemented by strategy 1
new System(new Strategy2()) // The system implemented by strategy 2
Copy the code

disadvantages

Don’t go overboard and use one policy pattern per branch, as this will result in too many policy classes. When branching logic is simple, clear and easy to maintain, there is no need to use policy pattern abstraction.

conclusion

Strategic patterns are very important abstract thinking. We must first realize that there are many solutions to a problem before we can realize the existence of strategic patterns. The strategy pattern can be considered when a problem requires different strategies, which are relatively complex, and new strategies may be developed in the future.

The discussion address is: Close reading Design Patterns – Strategy Strategy Patterns · Issue #304 · DT-fe /weekly

If you’d like to participate in the discussion, pleaseClick here to, with a new theme every week, released on weekends or Mondays. Front end Intensive Reading – Helps you filter the right content.

Pay attention to the front end of intensive reading wechat public account

Copyright Notice: Freely reproduced – Non-commercial – Non-derivative – Remain signed (Creative Commons 3.0 License)