strategy pattern


In Strategy Pattern, the behavior of a class or its algorithm can be changed at runtime. This type of design pattern is a behavioral pattern.

In the strategy pattern, we create objects that represent various strategies and a context object whose behavior changes as the strategy object changes. The strategy object changes the execution algorithm of the context object.

Introduction

Intent:Define a series of algorithms, encapsulate them one by one, and make them interchangeable.

Main solution: When there are multiple similar algorithms, the complexity and difficulty in maintenance caused by using if...else.

When to use: A system has many, many classes, and what distinguishes them is only their direct behavior.

How to solve: Encapsulate these algorithms into classes one by one and replace them arbitrarily.

Key code: Implement the same interface.

Application examples: 1. Zhuge Liang’s tips, each tip is a strategy. 2. The way to travel, choose to ride a bicycle or take a car. Each way of traveling is a strategy. 3. LayoutManager in JAVA AWT.

Advantages: 1. The algorithm can be switched freely. 2. Avoid using multiple conditional judgments. 3. Good scalability.

Disadvantages: 1. Strategy categories will increase. 2. All strategy classes need to be exposed to the outside world.

Usage scenarios: 1. If there are many classes in a system, and the difference between them is only their behavior, then the strategy pattern can be used to dynamically make an object perform many behaviors. Choose a behavior. 2. A system needs to dynamically choose one of several algorithms. 3. If an object has many behaviors, these behaviors have to be implemented using multiple conditional selection statements without using appropriate patterns.

Notes: If a system has more than four strategies, you need to consider using mixed mode to solve the problem of policy class expansion.

Implementation

We will create a Strategy interface that defines the activity and an entity strategy class that implements the Strategy interface. Context is a class that uses a certain strategy.

StrategyPatternDemo, our demo class uses Context and a strategy object to demonstrate the behavior changes of a Context when the strategies it configures or uses change.

strategy_pattern_uml_diagram.jpg

Step 1

Create an interface.

Strategy.java

public interface Strategy {
   public int doOperation(int num1, int num2);
}

Step 2

Create an entity class that implements the interface.

OperationAdd.java

public class OperationAdd implements Strategy{
   @Override
   public int doOperation(int num1, int num2) {
      return num1 + num2;
   }
}

OperationSubstract.java

public class OperationSubstract implements Strategy{
   @Override
   public int doOperation(int num1, int num2) {
      return num1 - num2;
   }
}

OperationMultiply.java

public class OperationMultiply implements Strategy{
   @Override
   public int doOperation(int num1, int num2) {
      return num1 * num2;
   }
}

Step 3

Create the Context class.

Context.java

public class Context {
   private Strategy strategy;

   public Context(Strategy strategy){
      this.strategy = strategy;
   }

   public int executeStrategy(int num1, int num2){
      return strategy.doOperation(num1, num2);
   }
}

Step 4

Use Context to see when it changes the strategyStrategy## Behavior changes when #.

StrategyPatternDemo.java

public class StrategyPatternDemo {
   public static void main(String[] args) {
      Context context = new Context(new OperationAdd());		
      System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

      context = new Context(new OperationSubstract());		
      System.out.println("10 - 5 = " + context.executeStrategy(10, 5));

      context = new Context(new OperationMultiply());		
      System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
   }
}

Step 5

Verify the output.

10 + 5 = 15
10 - 5 = 5
10 * 5 = 50