Design pattern - strategy pattern

About policy mode

The policy pattern is to prepare a set of algorithms and encapsulate them into a series of policy classes as a subclass of an abstract policy class. The focus of strategy mode is not how to implement algorithms, but how to organize these algorithms, so as to make the program structure more flexible, more maintainable and extensible.

Definition and characteristics of strategic mode

Definition of Strategy pattern: this pattern defines a series of algorithms and encapsulates each algorithm so that they can be replaced with each other, and the change of the algorithm will not affect the customers using the algorithm. The policy pattern belongs to the object behavior pattern. By encapsulating the algorithm, it separates the responsibility of using the algorithm from the implementation of the algorithm, and delegates it to different objects to manage these algorithms.

The main advantages of the strategy model are as follows.

      1. Multiple conditional statements, such as if, can be avoided Else statement, switch Case statement.
      2. Policy pattern provides a series of reusable algorithm families. Proper use of inheritance can transfer the public code of the algorithm family to the parent class, so as to avoid repeated code.
      3. The policy pattern can provide different implementations of the same behavior, and customers can choose different policies according to different time or space requirements.
      4. The strategy mode provides perfect support for the opening and closing principle, and can flexibly add new algorithms without modifying the original code.
      5. The policy pattern puts the use of the algorithm into the environment class, and the implementation of the algorithm is moved to the specific policy class, which realizes the separation of the two.

Its main disadvantages are as follows.

      1. The client must understand the differences between all policy algorithms in order to select the appropriate algorithm class in time.
      2. The policy mode creates many policy classes, which increases the difficulty of maintenance.

Structure and implementation of policy pattern

Structure of pattern

The main roles of the policy model are as follows.

    1. Abstract Strategy class: defines a public interface. Different algorithms implement this interface in different ways. Environmental roles use this interface to call different algorithms. Generally, it is implemented using interfaces or abstract classes.
    2. Concrete Strategy class: implements the interface of abstract policy definition and provides specific algorithm implementation.
    3. Context class: holds a reference to a policy class, which is finally called to the client.

Implementation example

public class StrategyPattern {
    public static void main(String[] args) {
        Context c = new Context();
        Strategy s = new ConcreteStrategyA();
        s = new ConcreteStrategyB();
//Abstract policy class
interface Strategy {
    public void strategyMethod();    //Strategy method
//Specific policy class A
class ConcreteStrategyA implements Strategy {
    public void strategyMethod() {
        System.out.println("Specific strategies A The policy method of is accessed!");
//Specific policy class B
class ConcreteStrategyB implements Strategy {
    public void strategyMethod() {
        System.out.println("Specific strategies B The policy method of is accessed!");
//Environment class
class Context {
    private Strategy strategy;
    public Strategy getStrategy() {
        return strategy;
    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    public void strategyMethod() {


Application scenario of policy mode

Policy patterns are used in many places. For example, container layout management in Java SE is a typical example. Each container in Java SE has a variety of layouts for users to choose from. In program design, the policy mode is often used in the following situations.

    1. When a system needs to dynamically select one of several algorithms, each algorithm can be encapsulated into a policy class.
    2. A class defines a variety of behaviors, and these behaviors appear in the form of multiple conditional statements in the operation of this class. Each conditional branch can be moved into their respective policy class to replace these conditional statements.
    3. Each algorithm in the system is completely independent of each other, and it is required to hide the implementation details of specific algorithms from customers.
    4. When the system requires that the customers using the algorithm should not know the data they operate, the policy mode can be used to hide the data structure related to the algorithm.
    5. Multiple classes only have different behaviors. You can use the policy mode to dynamically select the specific behavior to be executed at runtime.

Extension of policy mode

In a system using policy mode, when there are many policies, the client management algorithm of all policies will become very complex. If the policy factory mode is used in the environment class to manage these policy classes, the work complexity of the client will be greatly reduced

Directory of articles of this type: Design pattern summary

Posted by LankyMac on Sat, 16 Apr 2022 10:06:12 +0930