SkillPilotAI

SOLID principles

SOLID is five design principles (Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion) that let new features arrive as new code instead of edits to tested code. Here they are applied step by step to one checkout example.

20.1 The problem: one class, every job

C#
new Checkout().Pay("upi", 499);new Checkout().Pay("netbanking", 899); class Checkout{    public void Pay(string type, decimal amount)    {        if (type == "upi")            Console.WriteLine($"UPI: {amount}");        else if (type == "card")            Console.WriteLine($"Card: {amount}");        else throw new NotSupportedException(type);        Console.WriteLine("SMS sent");    }}
Output
$ dotnet run Checkout.csUPI: 499SMS sentUnhandled exception.System.NotSupportedException: netbanking   at Checkout.Pay(String type, ...)
  • Checkout does three jobs: choose the type, make the payment, send the SMS, so it has many reasons to change.
  • Every new payment method means opening this tested class and adding another else if.
  • One mistake there breaks the whole checkout. Goal: new feature = new code.

20.2 S: Single Responsibility

  • A class should have only one reason to change.
  • The SMS job moves into SmsNotifier; Checkout receives it through the constructor.
  • Changing the SMS format no longer touches Checkout.
C#
new Checkout(new SmsNotifier()).Pay("upi", 499); class Checkout(SmsNotifier sms){    public void Pay(string type, decimal amount)    {        Console.WriteLine($"{type}: {amount}");        sms.Send(amount);    }}class SmsNotifier{    public void Send(decimal amount) =>        Console.WriteLine($"SMS: {amount} paid");}
Output
$ dotnet run Checkout.csupi: 499SMS: 499 paid

20.3 O: Open/Closed

  • Open for extension, closed for modification.
  • IPaymentMethod has only Pay; Checkout takes an IPaymentMethod instead of a string.
  • Checkout calls method.Pay(amount) without knowing UPI or card: the if/else chain is gone.
C#
interface IPaymentMethod{    void Pay(decimal amount);} class Checkout(SmsNotifier sms){    public void Pay(IPaymentMethod method,        decimal amount)    {        method.Pay(amount);        sms.Send(amount);    }}
  • Adding NetBanking = one new class; not a single line of Checkout changes.
C#
var checkout = new Checkout(new SmsNotifier());checkout.Pay(new UpiPayment(), 499);checkout.Pay(new NetBankingPayment(), 899); class UpiPayment : IPaymentMethod{    public void Pay(decimal amount) =>        Console.WriteLine($"UPI: {amount}");}class NetBankingPayment : IPaymentMethod{    public void Pay(decimal amount) =>        Console.WriteLine($"NetBanking: {amount}");}
Output
$ dotnet run Checkout.csUPI: 499SMS: 499 paidNetBanking: 899SMS: 899 paid
From god class to SOLID checkout
  1. 1.Start: Checkout.Pay(string type) with if/else + SMS

  2. 2.S: move SMS into SmsNotifier

  3. 3.O: IPaymentMethod, one class per payment method

  4. 4.L + I: Refund only in a separate IRefundable

  5. 5.D: Checkout takes INotifier, chosen outside

  6. 6.New feature = new class, Checkout untouched

20.4 L: Liskov Substitution (the refund trap)

  • New requirement: refunds, so Refund is added to IPaymentMethod.
  • CodPayment (Cash on Delivery) cannot refund online, so it throws.
  • Any implementation put in place of the interface must work without surprises; CodPayment breaks the promise.
C#
IPaymentMethod[] payments =    [new UpiPayment(), new CodPayment()];foreach (var p in payments)    p.Refund(499); class CodPayment : IPaymentMethod{    public void Pay(decimal amount) =>        Console.WriteLine($"COD: {amount}");    public void Refund(decimal amount) =>        throw new NotSupportedException(            "No refund for COD");}
Output
$ dotnet run Refund.csUPI refund: 499Unhandled exception.System.NotSupportedException: No refund for COD   at CodPayment.Refund(Decimal amount)

20.5 I: Interface Segregation

  • Do not force a class to implement methods it does not use.
  • Move Refund into a small IRefundable; CodPayment implements only IPaymentMethod.
  • Putting COD into a refund list is now a compile error (CS0029): the bug is caught before running.
C#
interface IPaymentMethod{    void Pay(decimal amount);}interface IRefundable{    void Refund(decimal amount);}class CodPayment : IPaymentMethod{    public void Pay(decimal amount) =>        Console.WriteLine($"COD: {amount}");}// UpiPayment : IPaymentMethod, IRefundable

20.6 D: Dependency Inversion

  • Checkout still depends on the concrete SmsNotifier, and the business now wants WhatsApp.
  • INotifier interface; Checkout takes an INotifier.
  • Which notifier is used is decided outside (top level); Checkout does not change.
C#
INotifier notifier = new WhatsAppNotifier();var checkout = new Checkout(notifier);checkout.Pay(new UpiPayment(), 499); interface INotifier { void Send(decimal amount); } class Checkout(INotifier notifier){    public void Pay(IPaymentMethod method,        decimal amount)    {        method.Pay(amount);        notifier.Send(amount);    }}
Output
$ dotnet run Checkout.csUPI: 499WhatsApp: 499 paid
Dependency Inversion: three boxes
  1. 1.High level: Checkout holds the business rule

  2. 2.Abstractions: IPaymentMethod, INotifier

  3. 3.Details: UpiPayment, WhatsAppNotifier implement them

  4. 4.Top level creates objects and passes them into the constructor (DI)

Both sides depend on interfaces; ASP.NET Core's DI container does the wiring automatically

20.7 Summary: the five letters

LetterPrincipleIn the checkout example
SSingle Responsibility: one reason to changeSMS moved to SmsNotifier
OOpen/Closed: extend without modifyingNetBanking = new class implementing IPaymentMethod
LLiskov Substitution: implementations keep the contractCodPayment throwing on Refund breaks it
IInterface Segregation: small, focused interfacesRefund moved to IRefundable
DDependency Inversion: depend on abstractionsCheckout takes INotifier via the constructor

20.8 Common mistakes

  • Thinking SOLID means an interface for every class and a class for every method: S means one reason to change, not one method.
  • Adding an interface for a class with only one implementation and no testing need: over-engineering.
  • Treating SOLID as a rule book: apply it when a real change or testing need arrives.
  • Implementations throwing NotSupportedException: a sign Liskov (and usually ISP) is broken.

20.9 5 practical SOLID rules

  1. Two reasons to change? Split the class.
  2. if/else on a type? Think interface + new class.
  3. NotSupportedException in an implementation = L broken.
  4. Keep interfaces small and focused.
  5. Take dependencies through the constructor; create objects only at the top level.

20.10 Interview tips

  • Always answer SOLID with one example (payment checkout), letter by letter.
  • S: one reason to change; O: a new payment method is a new class, Checkout is not edited.
  • L: any implementation can replace the interface without surprises; I: small interfaces like IRefundable so COD writes no fake Refund.
  • D: high-level code depends on interfaces and gets dependencies through the constructor, which keeps it testable (ASP.NET Core DI).