Strategy Pattern Explained
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Strategy is a behavioral pattern that puts interchangeable behavior behind one interface and gives a context one implementation through composition. A cart, for example, can delegate to a DiscountPolicy rather than contain every discount formula. This separates the stable checkout flow from rules that change independently and makes each rule easier to test. The selection still has to happen somewhere, such as configuration, a registry, a factory, or dependency injection. I use Strategy when the variants share a real contract and are likely to grow or change. For one small stable branch, a conditional or lambda may be clearer than several new classes.
Strategy Pattern Explained
Direct Answer
Strategy is a behavioral pattern that represents interchangeable behavior behind a common interface and gives a context one of those implementations through composition. It helps when an operation has several meaningful variants that should change independently. Selection still exists—often in configuration, a factory, or a registry—and a simple conditional may remain clearer for a small stable case.
Common Mistakes & Pitfalls
- Using Strategy for one tiny stable branch: the added interface and selection code may be harder to read.
- Hiding a large switch in another class: selection remains a design responsibility even when behavior is separated.
- Forcing unlike workflows into one interface: the common contract should represent real substitutability.
- Assuming the context can never know a type: the useful boundary depends on ownership and change, not a slogan.
- Creating stateful shared strategies without a concurrency plan: lifecycle and thread safety still matter.
Check your understanding
Answer these without looking back at the article.
- How is Strategy different from simple polymorphism via inheritance?
- How does Spring use the Strategy pattern in its framework?
- Where should strategy selection live so the context stays closed?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.