Java Design Pattern Series (12) Strategy Pattern (Strategy)

Java Design Pattern Series (12) Strategy Pattern (Strategy)

The strategy pattern belongs to the behavior pattern of the object. The intent is to encapsulate a set of algorithms into separate classes with a common interface, making them interchangeable. The strategy pattern allows the algorithm to be changed without affecting the client.

The structure of the strategy pattern

The strategy mode is the packaging of the algorithm, which separates the responsibility of using the algorithm from the algorithm itself, and delegates the management to different objects. The strategy pattern usually wraps a series of algorithms into a series of strategy classes as a subclass of an abstract strategy class. In one sentence, it is: "Prepare a set of algorithms and encapsulate each algorithm so that they are interchangeable". The structure of the strategy pattern instance is explained below with a schematic implementation.

Figure 21-1 Structure of the Strategy Pattern

  • Strategy: Strategy interface, used to constrain a series of specific strategy algorithms. The Context uses this interface to invoke the algorithm defined by the concrete strategy implementation.

  • ConcreteStrategy: The specific strategy implementation, that is, the specific algorithm implementation.

  • Context: Context, responsible for interacting with the specific strategy class, usually the context will hold a real strategy implementation, the context can also allow the specific strategy class to obtain the context data, and even allow the specific strategy class to call back the context methods.

source code

(1) Strategy interface

/** 策略,定义算法的接口 */
public interface Strategy {
    /** 某个算法的接口,可以有传入参数,也可以有返回值 */
    public void algorithmInterface();
}

(2) Strategy implementation

/** 实现具体的算法 */
public class ConcreteStrategyA implements Strategy {
    public void algorithmInterface() {
        //具体的算法实现
    }
}

(3) Let's take a look at the implementation of the context. The sample code is as follows:

/** 上下文对象,通常会持有一个具体的策略对象 */
public class Context {
    /** 持有一个具体的策略对象 */
    private Strategy strategy;

    /** 构造方法,传入一个具体的策略对象 */
    public Context(Strategy aStrategy) {
        this.strategy = aStrategy;
    }

    /** 上下文对客户端提供的操作接口,可以有参数和返回值 */
    public void contextInterface() {
        //通常会转调具体的策略对象进行算法运算
        strategy.algorithmInterface();
    }
}

2. Summary

(1) Advantages and disadvantages of strategy mode

Advantages of Strategy Pattern

(1) The strategy pattern provides a way to manage related algorithm families. The hierarchy of policy classes defines a family of algorithms or behaviors. Proper use of inheritance can move common code into the parent class, thereby avoiding code duplication.

(2) Using the strategy pattern can avoid the use of multiple conditional (if-else) statements. Multiple conditional statements are not easy to maintain. It mixes the logic of which algorithm or behavior to take with the logic of the algorithm or behavior, all listed in a multiple conditional statement, which is more primitive and backward than the method of inheritance. .

Disadvantages of Strategy Pattern

(1) The client must know all the policy classes and decide which one to use. This means that clients must understand the difference between these algorithms in order to choose the right algorithm class at the right time. In other words, the strategy pattern only works when the client knows the algorithm or behavior.

(2) Since the strategy pattern encapsulates each specific strategy implementation as a class, if there are many alternative strategies, the number of objects will be considerable.


Record a little bit every day. Content may not be important, but habits are!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326623869&siteId=291194637