This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

A concept,

As a kind of software design Pattern, Strategy Pattern refers to that an object has a certain behavior, but in different scenarios, the behavior has different implementation algorithms.

For example, when video website users watch videos, ordinary users, members, super members, limited members “enjoy” the advertising time, the choice of video quality is very different. Simply put, a policy pattern is a pattern of object behavior that defines a family of algorithms, encapsulates them separately, and makes them interchangeable with each other. In this pattern, the changes in the algorithm do not affect the users who use the algorithm.

Therefore, the characteristics of the strategy pattern are:

  • Defines a family of algorithms (business rules);
  • Encapsulates each algorithm;
  • This family of algorithms can be interchangeable.

2. Code model

The so-called Strategy model must contain a number of Strategy algorithms, we first define an abstract Strategy Strategy.

public interface Strategy {
    /**
     * strategy details
     */
    void strategyImplementation();
}
Copy the code

We provide a number of different implementation classes for this abstract strategy.

DefaultStrategy


class DefaultStrategy implements Strategy { /** * strategy details */ @Override public void strategyImplementation() { System.out.println("default strategy"); }}Copy the code

AnotherStrategy

class AnotherStrategy implements Strategy { /** * strategy details */ @Override public void strategyImplementation() { System.out.println("another strategy"); }}Copy the code

We have the strategy algorithms, so how do we make them easy for the client to use? With a Context, of course.

public class Context { Strategy strategy = null; public Context(Strategy strategy) { this.strategy = strategy; } public void doStrategy() { strategy.strategyImplementation(); }}Copy the code

The method used by the client.

    public static void main(String[] args) {
        final Context context = new Context(new DefaultStrategy());
        context.doStrategy();
        final Context anotherContext = new Context(new AnotherStrategy());
        anotherContext.doStrategy();
    }
Copy the code

Often we don’t use this in a production environment. Why? This is because we often choose strategies dynamically based on actual needs, rather than executing multiple policies at once.

Modify the client usage to dynamically assign policies by obtaining a policy point.

    static void doStrategy(String strategyPoint){
        Context context;
        if(strategyPoint.equals("default")){
            context = new Context(new DefaultStrategy());
        }else if(strategyPoint.equals("another")){
            context = new Context(new AnotherStrategy());
        }else{
            throw new IllegalArgumentException("Unknown strategy");
        }
        context.doStrategy();
    }
Copy the code

It is clear that this logic will be modified as policies continue to be added. In the next article, we will talk about how to optimize this part of the code using the factory pattern + strategy pattern.

Third, summary

What application scenarios does the policy pattern fit into?

  • Suppose there are many classes in a system that differ only in their behavior.
  • A system needs to dynamically choose one of several algorithms.

The policy pattern emphasizes the need to introduce the pattern for management because of the dynamic change of the business scenario. If your algorithm rarely changes, then there is no need to introduce the policy pattern. The forced introduction will increase the amount of code and logical complexity. And the policy pattern is not particularly user-friendly because it has to choose different policies, so it has to know the differences between them.