Java Strategy Pattern
In this page:
What is the Strategy Pattern?
The Strategy pattern defines a family of interchangeable algorithms behind a common interface, letting the algorithm used by an object be selected or swapped independently of the code that uses it.
Example: What is the Strategy Pattern?
public class Main {
interface Strategy { int execute(int a, int b); }
public static void main(String[] args) {
Strategy add = (a, b) -> a + b; // interchangeable behind a common interface
System.out.println(add.execute(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Defining Interchangeable Algorithms
Each concrete strategy implements the same method signature but with completely different internal logic, so code that depends on the strategy interface works identically no matter which concrete strategy is actually plugged in.
Example: Defining Interchangeable Algorithms
public class Main {
interface Strategy { int execute(int a, int b); }
static class AddStrategy implements Strategy { public int execute(int a, int b) { return a + b; } }
static class MultiplyStrategy implements Strategy { public int execute(int a, int b) { return a * b; } }
public static void main(String[] args) {
Strategy s = new MultiplyStrategy(); // same signature, different internal logic
System.out.println(s.execute(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Switching Strategy at Runtime
A class using the Strategy pattern typically holds a reference to its current strategy and exposes a way to change it, allowing the algorithm to be swapped at runtime based on configuration, user choice, or other conditions.
Example: Switching Strategy at Runtime
public class Main {
interface Strategy { int execute(int a, int b); }
static class Calculator {
Strategy strategy;
void setStrategy(Strategy strategy) { this.strategy = strategy; } // swap at runtime
int calculate(int a, int b) { return strategy.execute(a, b); }
}
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.setStrategy((a, b) -> a - b);
System.out.println(calc.calculate(10, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Strategy with Lambdas
Since Java 8, a simple strategy can often be represented directly as a lambda expression implementing a functional interface like Function or Comparator, avoiding the boilerplate of a full interface and multiple implementing classes.
Example: Strategy with Lambdas
import java.util.function.BinaryOperator;
public class Main {
public static void main(String[] args) {
BinaryOperator<Integer> strategy = (a, b) -> a * b; // no separate class needed
System.out.println(strategy.apply(4, 5));
}
}
Login to try C/C++/Java/PHP code in the editor
Strategy vs If-Else Chains
Strategy is a common alternative to a long if-else or switch chain that selects behavior based on a type code -- each strategy is added as a new class or lambda without ever needing to modify the existing dispatch logic.
Example: Strategy vs If-Else Chains
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, java.util.function.BinaryOperator<Integer>> strategies = new HashMap<>();
strategies.put("add", (a, b) -> a + b);
strategies.put("sub", (a, b) -> a - b); // add more without touching existing dispatch code
System.out.println(strategies.get("add").apply(2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Java Design Patterns Introduction
- Java Singleton Pattern
- Java Factory Pattern
- Java Observer Pattern
- Java Builder Pattern
- Java MVC Architecture
- Java Adapter Pattern
- Java Decorator Pattern
- Java Strategy Pattern
- Java Command Pattern
- Java Facade Pattern
- Java Proxy Pattern
- Java Template Method Pattern
- Java Repository Pattern