JavaStrategy PatternCLEAN CODEOOP
Replacing If-Else Statements with the Strategy Pattern in Java
By Arapsih Güngör
- Published on
Sharing
Replacing If-Else Statements with the Strategy Pattern in Java
When your Java code is cluttered with multiple if-else statements based on input conditions, it becomes hard to maintain and extend. The Strategy Pattern offers a clean solution by encapsulating algorithms into separate classes, allowing you to select the appropriate behavior at runtime.
Traditional If-Else Approach
public void processInput(String input) {
if ("ADD".equals(input)) {
// Perform addition
} else if ("SUBTRACT".equals(input)) {
// Perform subtraction
} else if ("MULTIPLY".equals(input)) {
// Perform multiplication
} else {
// Default action
}
}
Problems with this approach:
- Maintainability Issues: Adding new operations requires modifying existing code.
- Violation of Open/Closed Principle: The code is not closed for modification.
Implementing the Strategy Pattern
1. Define a Strategy Interface
public interface OperationStrategy {
void execute(int a, int b);
}
2. Create Concrete Strategy Classes
public class AdditionStrategy implements OperationStrategy {
@Override
public void execute(int a, int b) {
System.out.println("Sum: " + (a + b));
}
}
public class SubtractionStrategy implements OperationStrategy {
@Override
public void execute(int a, int b) {
System.out.println("Difference: " + (a - b));
}
}
public class MultiplicationStrategy implements OperationStrategy {
@Override
public void execute(int a, int b) {
System.out.println("Product: " + (a * b));
}
}
3. Create a Context to Select Strategies
import java.util.HashMap;
import java.util.Map;
public class OperationContext {
private static final Map<String, OperationStrategy> strategies = new HashMap<>();
static {
strategies.put("ADD", new AdditionStrategy());
strategies.put("SUBTRACT", new SubtractionStrategy());
strategies.put("MULTIPLY", new MultiplicationStrategy());
}
public static void executeStrategy(String operation, int a, int b) {
OperationStrategy strategy = strategies.getOrDefault(operation, (x, y) -> {
System.out.println("Invalid operation");
});
strategy.execute(a, b);
}
}
4. Using the Strategy Pattern
public void processInput(String operation, int a, int b) {
OperationContext.executeStrategy(operation, a, b);
}
Benefits of Using the Strategy Pattern
- Extensibility: Easily add new operations without changing existing code.
- Maintainability: Cleaner code with separate classes for each operation.
- Flexibility: Select and execute strategies at runtime based on input.
Conclusion
By replacing multiple if-else statements with the Strategy Pattern, your code becomes more organized and scalable, adhering to solid design principles. This approach not only simplifies the addition of new functionalities but also makes your codebase easier to understand and maintain.
Resources
- "Design Patterns: Elements of Reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
- Strategy Design Pattern in Java
- Strategy