Strategy Pattern in Java: Definition, Example, and Usage
Strategy Pattern in Java is a design pattern that lets you define a family of algorithms, put each one in a separate class, and make them interchangeable. It helps change the behavior of a class at runtime by using different strategy objects without changing the class itself.How It Works
Imagine you have a remote control that can change the way a robot moves: walking, running, or jumping. Instead of building a new robot for each movement, you just swap the movement mode on the remote. The Strategy Pattern works the same way in programming. It lets you create different algorithms (strategies) as separate classes and switch them easily.
In Java, you define a common interface for all strategies. Then, the main class holds a reference to this interface and delegates the work to the current strategy object. This way, you can change the behavior of the main class by simply changing the strategy object it uses, without modifying the main class code.
Example
This example shows a simple payment system where different payment methods (Credit Card, PayPal) are strategies. The PaymentStrategy interface defines the method, and each payment type implements it. The ShoppingCart uses a strategy to process payment.
interface PaymentStrategy { void pay(int amount); } class CreditCardPayment implements PaymentStrategy { private String cardNumber; public CreditCardPayment(String cardNumber) { this.cardNumber = cardNumber; } @Override public void pay(int amount) { System.out.println("Paid " + amount + " using Credit Card ending with " + cardNumber.substring(cardNumber.length() - 4)); } } class PayPalPayment implements PaymentStrategy { private String email; public PayPalPayment(String email) { this.email = email; } @Override public void pay(int amount) { System.out.println("Paid " + amount + " using PayPal account " + email); } } class ShoppingCart { private PaymentStrategy paymentStrategy; public void setPaymentStrategy(PaymentStrategy paymentStrategy) { this.paymentStrategy = paymentStrategy; } public void checkout(int amount) { paymentStrategy.pay(amount); } } public class StrategyPatternDemo { public static void main(String[] args) { ShoppingCart cart = new ShoppingCart(); cart.setPaymentStrategy(new CreditCardPayment("1234567890123456")); cart.checkout(100); cart.setPaymentStrategy(new PayPalPayment("user@example.com")); cart.checkout(200); } }
When to Use
Use the Strategy Pattern when you have multiple ways to perform a task and want to switch between them easily without changing the main code. It is helpful when you want to avoid many if-else or switch statements that select behavior.
Real-world examples include payment methods, sorting algorithms, or different ways to compress files. It makes your code flexible, easier to maintain, and open for extension but closed for modification.
Key Points
- The Strategy Pattern defines a family of algorithms and makes them interchangeable.
- It uses composition to change behavior at runtime.
- It helps avoid complex conditional statements.
- Each strategy implements a common interface.
- The main class delegates work to the strategy object.