Mastering "D" in S.O.L.I.D
Dependency Inversion Principle

“High-level modules should not depend on low-level modules”
Don’t tightly couple your main code with details.
Both should depend on abstractions (like interfaces or abstract classes).“
Daily Life Example
Imagine:
High-level module = TV Remote
Low-level module = TV
Without Dependency Inversion:
Remote directly knows about SamsungTV and its buttons
This means the Remote will only work with a Samsung TV.
If you bring an LG TV, you’ll need to change the Remote code.
This is tight coupling and bad design.
With Dependency Inversion:
- We create a TV interface that defines basic functions:
interface TV {
void turnOn();
void turnOff();
void changeChannel(int ch);
}
Now, SamsungTV and LGTV both implement this interface.
Remote depends on the TV interface, not a specific TV.
class Remote {
TV tv; // depends on abstraction
void pressPower() { tv.turnOn(); }
}
Now the Remote can work with any TV.
If you change the TV, Remote code does not need to change. ✅
Summary
DIP means: "High-level code should not depend on low-level code. Work via abstractions."
Real-life example: Remote depends on TV interface, not Samsung or LG TV.
Benefit: Flexible, maintainable, easy to extend in the future.



