This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

The strategy pattern

  • Policy mode:Define a set of algorithms and encapsulate each algorithm into a separate class with a common interface, thus making the algorithms interchangeable with each other
    • The policy mode is a behavioral mode, which allows the algorithm to switch freely without affecting the client
    • The policy pattern is a wrapper around an algorithm, separating the algorithm used from the algorithm itself and delegating management to different objects
    • The policy pattern typically wraps a set of algorithms into a set of policy classes as a subclass of an abstract policy class
  • Application scenarios of the policy mode:
    • When multiple algorithms or policies exist to implement a function, different algorithms or policies can be selected to implement the function according to the different environment or conditions
      • When a system needs to dynamically select one of several algorithms, each algorithm can be encapsulated in a policy class
      • A class defines multiple behaviors, and these behaviors occur in the class’s operations in the form of multiple conditional statements, which can be replaced by moving each conditional branch into its own policy class
      • Each algorithm strategy in the system is independent of each other, and the implementation details of the specific algorithm are required to be hidden from the client
      • The system requires that the customer using the algorithm should not know about the data being operated, and the data structures associated with the algorithm can be hidden using the policy pattern
      • The differences between classes are only differences in behavior, and you can use the policy pattern to select the behavior that needs to be executed while the system is running
    • For example, data sort strategy bubble sort, selection sort, insertion sort and binary tree sort

  • The policy pattern consists of three roles:
    • Context: Holds a reference to the abstract Strategy
    • Abstract Strategy: Abstract roles. Gives the interfaces required by all concrete policy classes, usually implemented by an interface or abstract class
    • ConcreteStrategy: Concrete policy class. Encapsulates the specific algorithm
  • Example of the policy pattern Strategy

Summary of strategy Pattern

  • Key points of the strategy pattern:
    • How to implement the algorithm
    • It’s about how do you organize, how do you call a set of algorithms, so that the structure of your program is more flexible, more maintainable, more scalable
  • Equality of the algorithm:
    • One of the characteristics of the strategy pattern is the equality of the strategies of each algorithm
    • For a series of algorithms to be equal, only such algorithms can be interchangeable with each other
    • In the policy pattern, the specific policy algorithms are independent of each other in the implementation and have no dependence on each other
  • Uniqueness of operation strategy:
    • The policy can only use one specific policy implementation object at any point in time during the program’s run
    • The policy mode can dynamically switch between different specific algorithm policies, but only one specific algorithm policy can be used at a point in time
  • Public behavior in policy mode:
    • Typically, there is some common behavior between specific policies in a policy pattern
    • At this point, these behaviors that are common to each concrete policy should be placed in the common abstract Strategy class
    • In this case, the Strategy class can be implemented using the Abstract class Abstract instead of having to use an interface

Advantages of policy Mode

  • The policy pattern provides a way to manage a range of algorithms. The hierarchical structure of the Strategy class defines an algorithm or behavior family. Public code can be placed in the parent class Strategy through inheritance, so as to avoid code duplication
  • The policy pattern eliminates the use of multiple conditional if-else statements and improves project maintainability
  • The policy pattern provides different implementations of the same behavior, and the client can choose different implementation strategies based on different time and space requirements
  • The policy mode perfectly supports the open closed principle, and new algorithm policies can be flexibly added without modifying the original code
  • The strategy pattern puts the use of the algorithm into the environment class and the implementation of the algorithm into the specific policy class, which realizes the separation of the use of the algorithm and the implementation of the algorithm

Disadvantages of policy pattern

  • The policy pattern is only applicable when the client is aware of the specific algorithm policy or behavior. The client must know all the policy classes and decide which particular policy class to use. That means the client must understand the differences between these algorithms to decide which algorithm to use
  • The policy pattern encapsulates each specific algorithm policy into a class. If there are a large number of specific algorithm policies, there will be a large number of objects in the system

Strategic factory mode

  • In systems that use the policy pattern, when a system has a large number of policies, the client-managed policy algorithm can be complex, and these policy classes can be managed by using the policy factory pattern in the environment classes