Strategy Design Pattern in Java
1. Introduction
The Strategy Pattern is a behavioral pattern that allows selecting an algorithm’s behavior at runtime.
It defines a family of algorithms, encapsulates each one, and makes them interchangeable without altering the client code.
2. Real-World Analogy
Think about a Payment Gateway:
- A customer can pay using Credit Card, PayPal, or UPI.
- The payment method (strategy) can change at runtime without changing the payment processing system.
3. Structure of Strategy Pattern
- Strategy (Interface) → Defines the algorithm’s structure.
- Concrete Strategies → Implement different algorithms.
- Context → Uses a strategy object to execute the algorithm.
4. Custom Implementation Example
Let’s build a Payment Gateway System where payment method can be switched dynamically.
Step 1 – Strategy Interface
public interface PaymentStrategy {
void pay(double amount);
}
Step 2 – Concrete Strategies
CreditCardPayment
public class CreditCardPayment implements PaymentStrategy {
private String cardNumber;
public CreditCardPayment(String cardNumber) {
this.cardNumber = cardNumber;
}
@Override
public void pay(double amount) {
System.out.println("Paid $" + amount + " using Credit Card: " + cardNumber);
}
}
PayPalPayment
public class PayPalPayment implements PaymentStrategy {
private String email;
public PayPalPayment(String email) {
this.email = email;
}
@Override
public void pay(double amount) {
System.out.println("Paid $" + amount + " using PayPal: " + email);
}
}
UPIPayment
public class UPIPayment implements PaymentStrategy {
private String upiId;
public UPIPayment(String upiId) {
this.upiId = upiId;
}
@Override
public void pay(double amount) {
System.out.println("Paid $" + amount + " using UPI: " + upiId);
}
}
Step 3 – Context (PaymentProcessor)
public class PaymentProcessor {
private PaymentStrategy paymentStrategy;
public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
this.paymentStrategy = paymentStrategy;
}
public void processPayment(double amount) {
paymentStrategy.pay(amount);
}
}
Step 4 – Client Code
public class StrategyPatternDemo {
public static void main(String[] args) {
PaymentProcessor processor = new PaymentProcessor();
processor.setPaymentStrategy(new CreditCardPayment("1234-5678-9012-3456"));
processor.processPayment(250.75);
processor.setPaymentStrategy(new PayPalPayment("user@example.com"));
processor.processPayment(120.50);
processor.setPaymentStrategy(new UPIPayment("user@upi"));
processor.processPayment(300.00);
}
}
5. Output
Paid $250.75 using Credit Card: 1234-5678-9012-3456
Paid $120.5 using PayPal: user@example.com
Paid $300.0 using UPI: user@upi
6. Advantages
- Reduces conditional logic in code.
- Makes algorithms interchangeable at runtime.
- Promotes Open/Closed Principle — easy to add new strategies without changing existing code.
7. Real-World Use Cases
- Payment processing (Credit Card, PayPal, UPI).
- Sorting algorithms (QuickSort, MergeSort, BubbleSort).
- Compression utilities (ZIP, RAR, GZIP).
Navigation systems (Car, Walking, Biking routes).
Next -Template Method Design Pattern in Java