C# Interview Question #153
What is the Strategy Pattern?
Design Patterns & Production Architecture Senior Advanced
Quick Interview Answer
The Strategy Pattern defines interchangeable implementations of an algorithm or business rule behind a common contract.
Detailed Explanation
The Strategy Pattern defines interchangeable implementations of an algorithm or business rule behind a common contract.
The caller depends on the strategy abstraction rather than using a large conditional statement for every behavior.
It is useful for payment methods, pricing rules, shipping calculations, notification channels, discount policies, and similar scenarios where the algorithm can vary independently.
Code Example
public interface IDiscountStrategy
{
decimal Calculate(decimal total);
}
public class RegularDiscount : IDiscountStrategy
{
public decimal Calculate(decimal total)
=> total * 0.05m;
}
public class PremiumDiscount : IDiscountStrategy
{
public decimal Calculate(decimal total)
=> total * 0.15m;
}