Learn from Chunhui - strategy model

In Strategy Pattern, the behavior of a class or its algorithm can be changed at run time. This type of design pattern belongs to behavioral pattern.

In the policy pattern, we create objects representing various policies and a context object whose behavior changes with the change of policy objects. The policy object changes the execution algorithm of the context object.


Intention: define a series of algorithms, encapsulate them one by one, and make them interchangeable.

Main solution: when there are many similar algorithms, use if Else brings complexity and difficult to maintain.

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

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

Key code: implement the same interface.

Application examples: 1. Zhuge Liang's knapsack, each knapsack is a strategy. 2. The way of travel, choose to ride a bike, take a car, each way of travel is a strategy. 3. LayoutManager in JAVA AWT.

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

Disadvantages: 1. The number of policy classes will increase. 2. All policy classes need to be exposed.

Usage scenario: 1. If there are many classes in a system, and the only difference between them is their behavior, then using the policy pattern can dynamically make an object choose one behavior among many behaviors. 2. A system needs to dynamically choose one of several algorithms. 3. If an object has a lot of behaviors, without appropriate patterns, these behaviors can only be realized by multiple conditional selection statements.

Note: if a system has more than four policies, you need to consider using mixed mode to solve the problem of policy class expansion.


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

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

Step 1

Create an interface.

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

Step 2

Create an entity class that implements the interface.

(1) Addition implementation class

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

(2) Subtraction implementation class

public class OperationSubtract implements Strategy{
   public int doOperation(int num1, int num2) {
      return num1 - num2;

(3) Multiplication implementation class

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

Step 3

Create Context class and use class

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 the behavior changes when it changes Strategy.

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 OperationSubtract());      
      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

Execute the program and output the results:

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


Tags: Design Pattern

Posted by pyr02k1 on Sun, 08 May 2022 23:22:17 +0300