Mid
From PDF
SOLID
Design Patterns & SOLID
How would you implement Strategy pattern in .NET?
Short answer: Example: Payment strategy selection // Strategy Interface public interface IPaymentStrategy
Example code
{ void Pay(decimal amount); } // Concrete Strategies public class CreditCardPayment : IPaymentStrategy
{
public void Pay(decimal amount) => Console.WriteLine($"Paid {amount} using Credit Card"); }
public class PayPalPayment : IPaymentStrategy
{
public void Pay(decimal amount) => Console.WriteLine($"Paid {amount} using PayPal"); } // Context public class PaymentContext
{
private IPaymentStrategy _paymentStrategy;
public PaymentContext(IPaymentStrategy paymentStrategy)
{
_paymentStrategy = paymentStrategy;
}
public void ExecutePayment(decimal amount)
{ _paymentStrategy.Pay(amount); }
} Usage: var context = new PaymentContext(new PayPalPayment()); context.ExecutePayment(200); // Outputs: Paid 200 using PayPal
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