← Back to Advanced Java Course | Chapter 6: Design Patterns | Lesson 9 of 14

Java 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.

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?

java
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));
	}
}

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

java
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));
	}
}

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

java
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));
	}
}

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

java
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));
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.