DEV Community

Viktor Taraskevych
Viktor Taraskevych

Posted on

What's the difference between Dependency Inversion and Liskov Substitution — or how to stay a good parent?

Both principles are about replacing one caregiver with another. The difference is who breaks things.

DIP — the family's responsibility
Imagine a child who can only fall asleep if Mom sings that specific song.
Mom leaves for a trip — and nobody can replace her. Not Dad, not Grandma, not a nanny.
The problem isn't the others. The problem is that the family built everything around one specific person.
DIP says: depend on the role ("someone who puts the child to sleep"), not on the person. Then who shows up is just a detail.

👉 Violating DIP = you can't replace anyone.

LSP — the caregiver's responsibility
Now the family did everything right: any caregiver is welcome.
A nanny arrives and says: "I don't feed kids" — or "I only work with kids over five."
The family couldn't have prevented this. The nanny accepted the role but didn't keep its promises.
LSP says: if you take the role, you keep the contract. Your style can differ, your obligations can't.

👉 Violating LSP = you replaced someone, and it broke.

In short:
DIP — can we swap at all? (the family's design)
LSP — is the swap safe? (the caregiver's behavior)

SOLID #SoftwareEngineering #CleanCode #OOP #ProgrammingHumor

Top comments (0)