100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the Strategy Pattern?

Understand the Strategy design pattern with a Java payment example, benefits, and interview-ready comparisons to State pattern.

mediumQ23 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

The Strategy pattern defines a family of interchangeable algorithms, encapsulates each one behind a common interface, and lets the client swap the algorithm at runtime without changing the code that uses it.

Instead of a large conditional choosing behavior, a context object holds a reference to a strategy interface and delegates the work to whichever concrete strategy is injected. New algorithms are added by writing a new class implementing the interface, not by editing existing code, which follows the open-closed principle. It is widely used for things like sorting comparators, payment methods, pricing rules, and compression algorithms where the "how" varies but the "what" stays the same.

  • Removes large conditional/switch blocks choosing behavior
  • New algorithms added without modifying existing code
  • Algorithms testable in isolation from the context
  • Behavior swappable at runtime, even per request

AI Mentor Explanation

A captain does not hardcode one bowling plan for the whole match; instead they hold a "bowling strategy" slot and plug in whichever bowler suits the situation — pace for the new ball, spin for a turning pitch, a defensive line at the death. The team’s overall game plan (the context) never changes, only which strategy object is currently plugged in. Adding a new bowling style later means training a new bowler, not rewriting the captain’s whole game plan.

Step-by-Step Explanation

  1. Step 1

    Define the strategy interface

    Declare a common method signature (e.g. execute()) that every algorithm variant will implement.

  2. Step 2

    Implement concrete strategies

    Write one class per algorithm variant, each implementing the shared interface.

  3. Step 3

    Give the context a strategy reference

    The context class holds a reference to the interface, not a concrete implementation, usually injected via constructor or setter.

  4. Step 4

    Delegate at call time

    The context calls the interface method; the concrete strategy currently assigned determines the actual behavior.

What Interviewer Expects

  • A clear contrast with hardcoded if/else or switch-based behavior selection
  • Understanding that the context depends on an abstraction, not a concrete class
  • A real example — payment methods, sorting comparators, pricing rules
  • Awareness of the open-closed principle connection

Common Mistakes

  • Confusing Strategy with State pattern (State changes based on internal transitions, Strategy is chosen externally)
  • Hardcoding the strategy selection with conditionals, defeating the pattern’s purpose
  • Not injecting the strategy, instead instantiating it inside the context
  • Forgetting that strategies should be stateless and interchangeable

Best Answer (HR Friendly)

The Strategy pattern lets you swap out an algorithm at runtime by hiding each version behind a common interface. Instead of one class full of if-else logic for every case, you get small, focused classes for each behavior, which makes the system easier to extend and test.

Code Example

Strategy pattern for payment processing
interface PaymentStrategy {
    void pay(double amount);
}

class CreditCardPayment implements PaymentStrategy {
    public void pay(double amount) {
        System.out.println("Paid " + amount + " via credit card");
    }
}

class PayPalPayment implements PaymentStrategy {
    public void pay(double amount) {
        System.out.println("Paid " + amount + " via PayPal");
    }
}

class Checkout {
    private PaymentStrategy strategy;
    Checkout(PaymentStrategy strategy) { this.strategy = strategy; }
    void setStrategy(PaymentStrategy strategy) { this.strategy = strategy; }
    void completeOrder(double amount) { strategy.pay(amount); }
}

// Usage
Checkout checkout = new Checkout(new CreditCardPayment());
checkout.completeOrder(49.99);
checkout.setStrategy(new PayPalPayment());
checkout.completeOrder(19.99);

Follow-up Questions

  • How does the Strategy pattern differ from the State pattern?
  • How would you avoid instantiating a new strategy object on every call?
  • How does dependency injection relate to implementing Strategy?
  • When would a lambda or function reference replace a full Strategy class hierarchy?

MCQ Practice

1. The Strategy pattern primarily solves the problem of?

Strategy encapsulates interchangeable algorithms behind a common interface, chosen at runtime.

2. In the Strategy pattern, the context class should?

The context depends on the abstraction and receives the concrete strategy via injection, keeping it decoupled.

3. Which principle does Strategy directly support?

New algorithms are added as new classes without modifying existing context code, which is the open-closed principle.

Flash Cards

What problem does Strategy solve?Selecting an interchangeable algorithm at runtime without conditional logic scattered through the code.

What does the context hold a reference to?The strategy interface, not a concrete implementation — the concrete class is injected.

How is a new algorithm added?By writing a new class implementing the strategy interface, without modifying existing code.

Strategy vs State pattern?Strategy is chosen externally by the client; State changes internally based on the object’s own transitions.

1 / 4

Continue Learning