“This is the 18th day of my participation in the August Gwen Challenge.

Welcome friends to search “Andy Hui” on wechat to pay attention to a wave!

Write some thoughts of the programmer, hope it will help you.

The Strategy of Design Patterns

Introduction to Policy Mode

The strategy pattern is a behavior design pattern that allows you to define a series of algorithms and place each algorithm in a separate class so that the objects of the algorithm are interchangeable.

The strategy pattern suggests identifying classes responsible for accomplishing a particular task in many different ways and then extracting the algorithms within them into a separate set of classes called policies.

It transforms a set of behaviors into objects and makes them interchangeable within the original context object, called the context, which contains references to the policy object and dispatches the execution behavior to the policy object. To change the way the context does its job, other objects can use another object to replace the currently linked policy object.

It has a context class that associates policies, stores references to each policy through a member variable, and is only responsible for delegating work to the connected policy object. This class does not need to know about policies, it just needs to interact with all policies through the same common interface.

Policy pattern structure

  • 1. Context

It maintains references to specific policies and communicates with that object only through the policy interface.

  • 2. Policy interface

A common interface for all specific policies that declares a context for methods to enforce the policy.

  • 3. Specific strategies

Different strategies for implementing the algorithms used in the context. When the context needs to run the algorithm, it calls the execution method on its attached policy object. (The context does not know the policy and does not care which policy it invoked.)

  • 4. Client

A specific policy object is created and passed to the context. The context uses a method to cause the client to replace the associated policy at run time.

Suitable for application Scenarios

1. When you want to use different algorithm variants in an object and want to switch algorithms at run time.

2. When there are many similar classes that are only slightly different in pointing to certain behaviors.

3. If the algorithm is not important in context logic, use this pattern to isolate the business logic of the class from the implementation details of the algorithm.

implementation

  • 1. Find the algorithm that changes the most frequently in the context class and declare a common policy interface for all variants of this algorithm.
  • Implement algorithms one by one in their respective classes (policy interfaces must be implemented).
  • 3. Store policy objects in context classes through member variables (context can only interact with policy objects through policy interfaces).
  • The client needs to associate the context class with the policy.

Meet the open and close principle; You can use composition instead of inheritance; The implementation of the algorithm can be separated from the code using the algorithm; You can switch algorithms within an object at run time.

Decoration mode lets you change the appearance of an object, while policy lets you change its essence.

Demo

Policy interfaces and classes that implement policies

Context class

Business logic and display results

As you can see from the display above, you can get different display results when calling the policy implementation class through the context class context in the Main method. In this way, we can use it according to the actual situation in the actual business development, which can achieve the effect of stripping the algorithm from the actual business and reducing the coupling between each other. Moreover, it also meets the open and closed principle. When we add a new algorithm in the later stage, we can realize the function without modifying the old algorithm.

Small remarks

Life is short, I don’t want to go after what I can’t see, I just want to catch what I can see.

Original is not easy, give a attention.

I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.