The strategy pattern

define

Define a set of algorithms, encapsulate each algorithm, and make them interchangeable. A Policy pattern allows an algorithm to change independently of the customers that use it. Also known as a Policy pattern

Simply put, the class calls different algorithms based on the specific policies that are passed in (implementing the policy interface, which contains an algorithm of its own). And let a Context class manage the policy.

Examples and code

// Define the policy interface and declare the corresponding algorithm
interface Strategy {
    void algorithm(a);
}

// Implement different policies
class FirstStrategy {
    @override
    public void algorithm(a) {... }}class SecondStrategy {
    @override
    public void algorithm(a) {... }}// Define the Context class to manage Strategy
class Context {
    private Strategy strategy;
    public void changeStrategy(Strategy strategy) {
        this.strategy = strategy;
    }
    
    public void algorithm(a) { Strategy.algorithm(); }}/ / use
public class Main {
    public static void main(String[] args) {
        Context work = new Context();
        work.changeStrategy(new FirstStrategy());
        work.algorithm();
        
        work.changStrategy(newSecondStrategy()); work.algorithm(); }}Copy the code

In Java development, ThreadPool uses this design pattern. The last parameter, RejectedExecutionHandler, is an interface that corresponds to our abstract policy and is used to pass in a specific rejection policy.

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)
Copy the code

Policy mode vs. state mode

The code is basically the same as the state pattern, but the overall idea of both design patterns is the same, to decouple. But these two correspond to different usage scenarios. Specifically, different states may change not just one calling method, but a series of different calling methods, other Context member variable values may change, and states may change spontaneously (finite state machines); The strategy mode is more specific. Different strategies only call different algorithms, and policies cannot be changed by themselves. Generally, different strategies must be used according to different scenarios.

conclusion

Compared to the state pattern, strategy pattern is more general, in practice, you can use the strategy pattern to encapsulate almost any type of rules, as long as heard in the process of analysis need to be in different application of business rules, can consider to use the strategy pattern processing, at this point the strategy pattern is contains the function of the state pattern, The strategy pattern is an important design pattern.