If object state changes, its behaviour will change.
Strategy Design Pattern — Simple Explanation
The Strategy Pattern means:
Define multiple ways (strategies) to do something, and allow the program to choose which one to use at runtime.
Real-world example 🛒
Imagine an online shopping app. You can pay using:
Payment
├── Credit Card
├── UPI
└── PayPalThe payment method changes, but the shopping/order logic doesn't need to change.
That's a perfect use case for Strategy Pattern.
1. Create a Strategy interface
interface PaymentStrategy {
void pay(double amount);
}This says:
Every payment strategy must provide a
pay()method.
2. Create different strategies
Credit Card
class CreditCardPayment implements PaymentStrategy {
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using Credit Card");
}
}UPI
class UpiPayment implements PaymentStrategy {
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using UPI");
}
}PayPal
class PayPalPayment implements PaymentStrategy {
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using PayPal");
}
}Now we have:
PaymentStrategy
│
┌───────┼────────┐
↓ ↓ ↓
CreditCard UPI PayPal3. Create the Context
The Context uses whichever strategy we give it.
class ShoppingCart {
private PaymentStrategy paymentStrategy;
public ShoppingCart(PaymentStrategy paymentStrategy) {
this.paymentStrategy = paymentStrategy;
}
public void checkout(double amount) {
paymentStrategy.pay(amount);
}
}4. Use it
public class Main {
public static void main(String[] args) {
PaymentStrategy strategy =
new UpiPayment();
ShoppingCart cart =
new ShoppingCart(strategy);
cart.checkout(1000);
}
}Output:
Paid ₹1000.0 using UPIWant Credit Card?
Just change the strategy:
PaymentStrategy strategy =
new CreditCardPayment();
ShoppingCart cart =
new ShoppingCart(strategy);
cart.checkout(1000);Output:
Paid ₹1000.0 using Credit CardWhy do we need Strategy Pattern?
Without Strategy Pattern, you might write:
void pay(String type, double amount) {
if (type.equals("UPI")) {
// UPI logic
}
else if (type.equals("CARD")) {
// Card logic
}
else if (type.equals("PAYPAL")) {
// PayPal logic
}
}As payment methods increase:
UPI
CARD
PAYPAL
NET_BANKING
APPLE_PAY
...the if-else becomes large and difficult to maintain.
With Strategy Pattern:
ShoppingCart
│
│ uses
↓
PaymentStrategy
│
┌───────────┼───────────┐
↓ ↓ ↓
UPI Card PayPalYou can add a new strategy without changing ShoppingCart.
class NetBankingPayment implements PaymentStrategy {
public void pay(double amount) {
System.out.println("Paid using Net Banking");
}
}🔥 The key idea
The behavior changes, but the main class doesn't.
STRATEGY
↓
"How should I perform this?"
↓
┌──────────┼──────────┐
↓ ↓ ↓
UPI CARD PAYPALThe client chooses the strategy:
new ShoppingCart(new UpiPayment());or:
new ShoppingCart(new CreditCardPayment());🧠 Interview answer
If the interviewer asks:
"What is Strategy Design Pattern?"
Say:
Strategy Pattern defines a family of interchangeable algorithms or behaviors, encapsulates each one behind a common interface, and allows the behavior to be selected at runtime without changing the client code.
Easy memory trick:
Strategy = "Different ways to do the same thing."
Examples:
Payment → UPI / Card / PayPal
Sorting → QuickSort / MergeSort
Compression → ZIP / GZIP
Notification → Email / SMS / Push
Navigation → Car / Bike / WalkingThe common interview clue is:
"I have many algorithms/behaviors and want to choose one at runtime."
Think Strategy Pattern.
No comments:
Post a Comment