Skip to main content

What does the Strategy pattern do and why is it needed?

The Strategy pattern is designed to encapsulate different behavior algorithms and allow them to be swapped at runtime, without changing the code of the object that uses them.


1. The Core Idea

When an object needs to perform some action (for example, sorting, payment, logging), but the way it's performed can vary, these ways are extracted into separate classes.

The object delegates the execution of the algorithm to the strategy, instead of deciding internally exactly which way to use.

This way, algorithms become interchangeable and independent.


2. Participants in the Pattern

  • Context - the object that uses the strategy. It holds a reference to the current strategy and calls its methods.
  • Strategy (Strategy Interface) - the common interface for all algorithms.
  • ConcreteStrategy - different implementations of the same behavior.

3. Example (Java)

Example: a payment system where you can pay by card or PayPal.

java
// Common strategy interface interface PaymentStrategy { void pay(int amount); } // Concrete strategies class CreditCardPayment implements PaymentStrategy { public void pay(int amount) { System.out.println("Paying " + amount + " by credit card"); } } class PayPalPayment implements PaymentStrategy { public void pay(int amount) { System.out.println("Paying " + amount + " via PayPal"); } } // Context class Order { private PaymentStrategy paymentStrategy; public Order(PaymentStrategy paymentStrategy) { this.paymentStrategy = paymentStrategy; } public void checkout(int amount) { paymentStrategy.pay(amount); } }

Usage:

java
Order order1 = new Order(new CreditCardPayment()); order1.checkout(100); // Paying by card Order order2 = new Order(new PayPalPayment()); order2.checkout(200); // Paying via PayPal

4. Why It's Needed

  1. To separate the choice of algorithm from its implementation. The context doesn't know exactly how the algorithm works, it just calls the strategy's method.
  2. To get rid of many if-else / switch constructs, where the way to use is manually selected.
  3. To easily add new algorithms, without changing existing code (the Open/Closed principle).
  4. To swap behavior at runtime, for example, changing the payment, sorting, or logging strategy "on the fly".

5. Advantages

  • Weakens coupling between the context and the algorithms.
  • Makes the code extensible: new strategies are added without changing the old ones.
  • Allows behavior to be changed dynamically.

6. Disadvantages

  • The number of classes increases (one per strategy).
  • The context needs to know when and which strategy to choose.

Conclusion

The Strategy pattern lets you:

  • encapsulate different algorithms,
  • make them interchangeable,
  • free the code from conditional constructs,
  • and dynamically change an object's behavior.

Summary:

Strategy is a way to choose an algorithm at runtime without changing the code that uses that algorithm.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.