Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Design Pattern — Strategy

7 min readMay 2, 2026

--

An in-depth discussion on the Strategy Design Pattern

You’ve written this code before — a growing if-else chain or a switch statement that picks a behavior based on some condition. It works until the third, fourth, fifth case gets added. The Strategy pattern exists to make that problem disappear.

What is the Strategy Design Pattern?

Strategy is a behavioral design pattern documented by the Gang of Four. It is also known as the Policy pattern.

The idea is simple: instead of hardcoding an algorithm inside a class, you define a family of algorithms, put each one in its own class, and let the client choose which one to use at runtime. The algorithm is selected — not baked in.

Think of a navigation app. You enter a destination, and it asks — drive, walk, or public transit? The app doesn’t have one giant method that handles all three. Each mode of transport is a separate strategy. The app picks one based on your choice and delegates the route calculation to it.

“The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.”
— Gang of Four

The key benefit is extensibility. Need a new algorithm? Add a new strategy class. The existing code doesn’t change. No if-else to modify, no switch to extend. Open for extension, closed for modification — the Open/Closed Principle in action.

Example

Consider a payment processing system. You need to support multiple payment methods — credit card, UPI, and net banking. Each has different processing logic.

Without Strategy, you’d end up with:

// #1 — The conditional approach (what Strategy eliminates)
public void processPayment(String type, double amount) {
if (type.equals("CREDIT_CARD")) {
// #2 — Credit card logic here
System.out.println("Processing credit card payment: " + amount);
} else if (type.equals("UPI")) {
// #3 — UPI logic here
System.out.println("Processing UPI payment: " + amount);
} else if (type.equals("NET_BANKING")) {
// #4 — Net banking logic here
System.out.println("Processing net banking payment: " + amount);
}
}
  • #1 — Every new payment method means another else if. This method grows forever.
  • #2, #3, #4 — All payment logic lives in one method. Testing one payment method means loading all of them.

This works for three types. It becomes painful at ten. The Strategy pattern fixes this.

Implementation

Step 1 — The Strategy interface

Define a common interface that all strategies implement.

// #1 — The contract all strategies must follow
public interface PaymentStrategy {
void pay(double amount); // #2 — One method, implemented differently by each strategy
}
  • #1 — This is the abstraction. The client code will depend on this interface, not on any concrete payment class.
  • #2 — Every strategy will implement pay() with its own logic.

Step 2 — Concrete strategies

Each payment method gets its own class.

// #1 — Credit card strategy
public class CreditCardPayment implements PaymentStrategy {
private String cardNumber;

public CreditCardPayment(String cardNumber) {
this.cardNumber = cardNumber; // #2 — Strategy-specific data
}

@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using Credit Card: " + cardNumber);
}
}

// #3 — UPI strategy
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);
}
}
  • #1, #3 — Each strategy is its own class. Self-contained. Independently testable.
  • #2 — Each strategy can carry its own data. Credit card needs a card number. UPI needs a UPI ID. They don’t interfere with each other.

Step 3 — The Context class

Get Vivek Mittal’s stories in your inbox

Join Medium for free to get updates from this writer.

The context holds a reference to a strategy and delegates the work to it.

// #1 — The context that uses a strategy
public class PaymentContext {
private PaymentStrategy strategy; // #2 — Holds the current strategy

public PaymentContext(PaymentStrategy strategy) {
this.strategy = strategy; // #3 — Strategy is set at runtime
}

public void setStrategy(PaymentStrategy strategy) {
this.strategy = strategy; // #4 — Can be changed at runtime
}

public void executePayment(double amount) {
strategy.pay(amount); // #5 — Delegates to the strategy
}
}
  • #2 — The context doesn’t know which strategy it’s using. It only knows the interface.
  • #3 — The strategy is injected via the constructor. The client decides which one.
  • #4 — The strategy can be swapped at runtime. Today credit card, tomorrow UPI — the context doesn’t care.
  • #5 — One line. No conditionals. The strategy does the work.

Step 4 — Usage

// #1 — Client picks the strategy
PaymentContext context = new PaymentContext(new CreditCardPayment("4111-1111-1111-1111"));
context.executePayment(1500.00); // #2 — Paid 1500.00 using Credit Card

// #3 — Switch strategy at runtime
context.setStrategy(new UpiPayment("vivek@upi"));
context.executePayment(500.00); // #4 — Paid 500.00 using UPI
  • #1 — The client creates the strategy and passes it to the context. This is the “runtime selection.”
  • #2 — The context doesn’t know it’s using a credit card. It just calls pay().
  • #3, #4 — Strategy swapped without changing the context class. No if-else anywhere.

UML Diagram

Press enter or click to view image in full size
UML diagram for Strategy Design Pattern — PaymentContext delegates to the PaymentStrategy interface, concrete strategies implement it

The PaymentContext holds a reference to the PaymentStrategy interface. CreditCardPayment, UpiPayment, and NetBankingPayment implement that interface. The client creates a concrete strategy and passes it to the context.

Problems addressed by Strategy Design Pattern

  • Eliminates growing if-else or switch chains for algorithm selection
  • Each algorithm is encapsulated in its own class — isolated, testable, replaceable
  • New algorithms can be added without modifying existing code (Open/Closed Principle)
  • The algorithm can be changed at runtime without restarting or redeploying
  • Separates the “what to do” (strategy) from the “when to do it” (context)

When to use Strategy?

  • Multiple algorithms for the same task — sorting, compression, encryption, payment processing. The task is the same, the approach varies.
  • Eliminating conditionals — when you have a switch or if-else that selects behavior based on a type or flag, that's a strong signal for Strategy.
  • Runtime flexibility — the behavior needs to change based on user input, configuration, or environment.
  • Testing in isolation — you want to test each algorithm independently without loading the others.

You don’t need Strategy when there are only two simple cases and they’re unlikely to grow. A straightforward if-else is fine. Don't over-engineer.

Issues with Strategy Design Pattern

Client must know all strategies — The client is responsible for picking the right strategy. It needs to be aware of all available options and understand the difference between them. This is a design trade-off, not a flaw — but it means adding a new strategy requires updating the client code too.

Increased class count — Every algorithm becomes its own class. For a system with 15 strategies, that’s 15 classes plus the interface. The code is cleaner, but the number of files grows. Keep the strategies organized in a package/namespace.

Overkill for simple cases — If you have two strategies and they’re unlikely to change, the pattern adds unnecessary abstraction. A simple if-else is more readable.

Communication overhead — The context passes data to the strategy via the interface method. Some strategies might not need all the parameters, while others might need more. Designing the interface to work for all strategies without becoming bloated requires thought.

Strategy vs. State

Strategy and State are structurally identical — both use composition and delegation through an interface. The difference is intent:

  • Strategy — the client explicitly picks which algorithm to use. The selection is external.
  • State — the object changes its own behavior internally based on its current state. The transition is automatic.

If the client chooses the behavior → Strategy. If the object changes its own behavior → State.

Summary

  • Strategy is a behavioral pattern that lets you select an algorithm at runtime
  • It eliminates conditional chains by encapsulating each algorithm in its own class
  • Three components: Strategy interface, Concrete strategies, Context
  • The context delegates work to the strategy without knowing the specifics
  • Adds extensibility — new strategies don’t require changes to existing code
  • Watch for increased class count and the requirement that clients know all strategies

Conclusion

Strategy is one of the most practical GoF patterns. It solves a problem every developer has faced — a growing block of conditionals that selects behavior based on some type or flag. The pattern replaces that with clean, isolated, swappable classes.

The real power shows up over time. When the fifth or tenth strategy gets added and no existing code changes, you appreciate the upfront investment. The context stays untouched. Each strategy is self-contained. The system grows without getting fragile.

If you’ve ever looked at a method with eight else if branches and felt uneasy — that unease is your instinct telling you to reach for Strategy.

Written & edited by — Vivek Mittal

https://www.linkedin.com/in/vivekmittal06/

--

Vivek Mittal
Vivek Mittal

Written by Vivek Mittal

I love tech and often write about what I learn

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development