What is the Strategy pattern and what problem does it solve?
Short answer: The Strategy pattern is a behavioral design pattern that defines a family of algorithms, encapsulates each one, and makes them interchangeable at runtime. It allows the algorithm to vary independently from clients that use it. Problem it solves: Avoids large if-else or switch statements when selecting behavior and promotes flexibility by decoupling the algorithm from the client using it.
Real-world example (ShopNest)
ShopNest payment fees use Strategy: IFeeCalculator with UPI/Card implementations chosen at runtime.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png