Strategy Design Pattern
The Strategy Design Pattern is a behavioral design pattern that defines a family of algorithms, encapsulates each one, and makes them interchangeable. This pattern allows a class's behavior to be selected at runtime.
Key Components of the Strategy Pattern
Strategy Interface – Defines a common interface for all supported algorithms.
Concrete Strategies – Implement different variations of the algorithm.
Context – Maintains a reference to a
Strategyand delegates execution to it.
Example: Payment System using Strategy Pattern
Imagine you need a system where users can pay using different methods like Credit Card, PayPal, and UPI.
Step 1: Create a Strategy Interface
javaCopyEdit// Strategy Interface
public interface PaymentStrategy {
void pay(int amount);
}
Step 2: Implement Concrete Strategies
javaCopyEdit// Concrete Strategy 1: Credit Card Payment
public 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: " + cardNumber);
}
}
// Concrete Strategy 2: PayPal Payment
public 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: " + email);
}
}
// Concrete Strategy 3: UPI Payment
public class UPIPayment implements PaymentStrategy {
private String upiId;
public UPIPayment(String upiId) {
this.upiId = upiId;
}
@Override
public void pay(int amount) {
System.out.println("Paid " + amount + " using UPI: " + upiId);
}
}
Step 3: Create a Context Class
javaCopyEdit// Context Class
public class PaymentContext {
private PaymentStrategy paymentStrategy;
// Set Strategy at Runtime
public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
this.paymentStrategy = paymentStrategy;
}
public void executePayment(int amount) {
if (paymentStrategy == null) {
throw new IllegalStateException("Payment strategy is not set.");
}
paymentStrategy.pay(amount);
}
}
Step 4: Use the Strategy Pattern
javaCopyEditpublic class StrategyPatternExample {
public static void main(String[] args) {
PaymentContext context = new PaymentContext();
// Pay using Credit Card
context.setPaymentStrategy(new CreditCardPayment("1234-5678-9876-5432"));
context.executePayment(1000);
// Pay using PayPal
context.setPaymentStrategy(new PayPalPayment("user@example.com"));
context.executePayment(500);
// Pay using UPI
context.setPaymentStrategy(new UPIPayment("user@upi"));
context.executePayment(300);
}
}
Benefits of the Strategy Pattern
✅ Encapsulation – Algorithms are encapsulated in separate classes.
✅ Flexibility – New strategies can be added without modifying existing code.
✅ Open/Closed Principle – The system is open for extension but closed for modification.
✅ Improved Maintainability – Each strategy class is independent and easily maintain code