I. Concept and introduction

define

By defining a set of encapsulated algorithms that do the same job in different implementations, they can be replaced with each other. The strategy pattern is a behavioral pattern, which encapsulates algorithms, delegates them to different objects to manage them, and separates the responsibility and implementation of algorithms.

Application scenarios

In practice, it has been found that policy patterns can be used to encapsulate not only algorithms but almost any type of rules. Whenever different business rules need to be applied in different situations, consider the possibility of using a policy pattern to handle this change.

Application, for example,

1. Store the target data in an array or a linked list. Or, using a binary tree…

2 breakfast is to eat pickled mustard bags, steamed dumplings, or eat brown sugar steamed buns.

The main advantages

1. When different behaviors are grouped in a class, it is often necessary to use conditional statements to select appropriate behaviors. If these behaviors are encapsulated in an independent Strategy class, conditional statements can be eliminated in the classes that use these behaviors (good extensibility and maintainability, which can reduce the use of judgment statements).

2. The policy pattern provides a series of reusable algorithm families. Proper use of inheritance can transfer the common code of the algorithm family to the parent class, thus avoiding duplication of code

3. The policy pattern puts the use of the algorithm in the Context environment class, while the implementation of the algorithm is moved to the specific policy class, realizing the separation of the two.

Main drawback

1. There are many policy classes, and all of them need to be exposed.

2. The client must understand the differences between all policy algorithms in order to select the appropriate algorithm class.

Ii. Structure and implementation

Pattern structure

Pattern implementation

// Abstract policy class
interface Strategy {
    public void strategyMethod(a);    // Strategy method
}
Copy the code
// Specific policy class A
class ConcreteStrategyA implements Strategy {
    public void strategyMethod(a) {
        System.out.println("Strategy A: Take A dumpling from the steamer."); }}Copy the code
// Specific policy class B
class ConcreteStrategyB implements Strategy {
    public void strategyMethod(a) {
        System.out.println("Execution strategy B: Take a brown sugar steamed bun from the steamer."); }}Copy the code
/ / environment
class Context {
    private Strategy strategy;

    public void startStrategy(Strategy strategy) {
        this.strategy = strategy; strategy.strategyMethod(); }}Copy the code
// Policy mode
public class StrategyPattern {
    public static void main(String[] args) {
        Context c = new Context();
        // Execute policy A
        Strategy strategyA = new ConcreteStrategyA();
        c.startStrategy(strategyA);
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        // Execute policy B
        Strategy strategyB = newConcreteStrategyB(); c.startStrategy(strategyB); }}Copy the code

Program run result

A: to execute A strategy from the steamer with A steamed dumpling -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- B: to execute A strategy with A brown sugar steamed buns from the steamerCopy the code

The Context here can be understood as the breakfast shop, but we (the client) have to find the corresponding food from the steamer ourselves. There’s something wrong with that.

Iii. Strategic Factory Mode (Expansion)

As mentioned earlier, one of the disadvantages of the policy pattern is that “the client must understand the differences between all the policy algorithms in order to choose the appropriate algorithm class.” The more algorithm classes there are, the more difficult the client’s job becomes…

So, is it possible to move the process of making these judgments from the client to somewhere else? This is where we have to think about our environment class Context. We can transform the Context class into a simple factory. In this way, the client does not have to select a good policy to pass to the Context. It only needs to pass the field representing the current situation, and let the Context class select the appropriate policy according to the different situations obtained.

In this example, we can interpret it as: in a breakfast shop, the customer just needs to shout, “I want to eat…” The boss will get it for you.

Without further ado, let’s tweak the previous code a little.

First is the environment class Context:

/ / environment
class Context {

    public static final String  DUMPLINGS = "Dumplings";
    public static final String STEAMED_BUN = "Brown sugar steamed bread";
    private Strategy strategy;

    public void startStrategy(String type) {
        System.out.println("Just a moment, I'll bring it to you.");
        switch (type) {
            // Execute policy A
            case DUMPLINGS:
                strategy = new ConcreteStrategyA();
                break;
            // Execute policy B
            case STEAMED_BUN:
                strategy = new ConcreteStrategyB();
                break;
            default:
                break; } strategy.strategyMethod(); }}Copy the code

Then there’s the client:

package Strategy;

import static Strategy.Context.DUMPLINGS;
import static Strategy.Context.STEAMED_BUN;

// Strategy factory mode
public class StrategyPattern {
    public static void main(String[] args) {
        Context c = new Context();
        // I'd like some dumplings
        System.out.println("I want to eat."+ DUMPLINGS);
        c.startStrategy(DUMPLINGS);
        
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        
        // I'd like steamed buns with brown sugar
        System.out.println("I want to eat."+ STEAMED_BUN); c.startStrategy(STEAMED_BUN); }}Copy the code

Program run result

I want to eat steamed dumplings guest officer wait A moment, I immediately give you implementation strategy A: take A steamed dumplings from the steamer ----------------- I want to eat brown sugar steamed bread guest officer wait A moment, I immediately give you implementation strategy B: take A brown sugar steamed bread from the steamerCopy the code

Ok, so much for the initial understanding of the strategy pattern. I am also new to design mode, I want to share my understanding and improve each other. If there are inappropriate places in the article, please point out, thank you for watching!