Sunday, 30 August 2026

State, Strategy - Sibling Design Patterns

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
  └── PayPal

The 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     PayPal

3. 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 UPI

Want Credit Card?

Just change the strategy:

PaymentStrategy strategy =
        new CreditCardPayment();

ShoppingCart cart =
        new ShoppingCart(strategy);

cart.checkout(1000);

Output:

Paid ₹1000.0 using Credit Card

Why 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       PayPal

You 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      PAYPAL

The 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 / Walking

The common interview clue is:

"I have many algorithms/behaviors and want to choose one at runtime."

Think Strategy Pattern.

No comments:

Post a Comment