What is the Strategy pattern and when would you use it?

The Strategy pattern is a behavioral pattern that encapsulates a family of interchangeable algorithms behind a common interface, letting the algorithm vary independently from the code that uses it. It's used whenever a task can be done multiple ways and you want to switch between those ways without touching the calling code.

Key Points

Example

An e-commerce checkout can use a DiscountStrategy interface with PercentageDiscount and FlatDiscount implementations, letting the order total be calculated the same way regardless of which discount type is active.

interface DiscountStrategy {
    double apply(double price);
}

class PercentageDiscount implements DiscountStrategy {
    public double apply(double price) { return price * 0.9; }
}

class NoDiscount implements DiscountStrategy {
    public double apply(double price) { return price; }
}

Interview Tip

A concise interview answer is

Strategy lets you encapsulate a family of algorithms behind one interface and swap between them at runtime without changing the client code. I'd use it any time there's more than one way to do something, like sorting, pricing, or payment processing, and I want to avoid a big conditional block picking the behavior.

Last updated: 16 July 2026

Previous question: What are the challenges of using the Observer pattern in multithreaded environments?

Next question: Can you explain the difference between the Strategy pattern and the Template Method pattern?

More Java Design Patterns interview questions

All Java Design Patterns questions