Strategy

Author      Ter-Petrosyan Hakob

The Strategy pattern is a behavioral design pattern. It helps you choose one of many ways (algorithms) to do a task at runtime. This makes your code more flexible and easier to change.

Strategy pattern is also known as Policy Pattern. We define multiple algorithms and let the client application pass the algorithm to be used as a parameter. One of the best examples of the Strategy pattern is Java’s Collections.sort() method, which takes a Comparator parameter. Depending on the implementation of Comparator, objects are sorted in different ways.

Simple Example

Imagine a calculator that can add or multiply numbers. We use Strategy so the calculator can switch between addition and multiplication.

Diagram

img2

Code

// Strategy interface
public interface Strategy {
    int execute(int a, int b);
}

// Concrete Strategy 1: Addition
public class AddStrategy implements Strategy {
    @Override
    public int execute(int a, int b) {
        return a + b;
    }
}

// Concrete Strategy 2: Multiplication
public class MultiplyStrategy implements Strategy {
    @Override
    public int execute(int a, int b) {
        return a * b;
    }
}

// Context class
public class Context {
    private Strategy strategy;

    // Set the strategy at runtime
    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    public int performOperation(int a, int b) {
        if (strategy == null) {
            throw new IllegalStateException("Strategy not set");
        }
        return strategy.execute(a, b);
    }
}

// Client code
public class Main {
    public static void main(String[] args) {
        Context ctx = new Context();

        // Use addition
        ctx.setStrategy(new AddStrategy());
        System.out.println("10 + 5 = " + ctx.performOperation(10, 5));

        // Switch to multiplication
        ctx.setStrategy(new MultiplyStrategy());
        System.out.println("10 × 5 = " + ctx.performOperation(10, 5));
    }
}

Output:

10 + 5 = 15
10 × 5 = 50

Sequence Diagram

img3

Key Terms

When to Use

Pros&Cons

Pros:

Cons: