Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 1.72 KB

dependency_inversion_principle.md

File metadata and controls

34 lines (23 loc) · 1.72 KB

Dependency Inversion Principle

Turning Your Code Upside Down for Good

In essence, the Dependency Inversion Principle encourages you to build your software on a foundation of abstractions, not concrete implementations, giving you a house that's not only strong but also adaptable to future changes.


How DIP works

  1. High-level modules depend on abstractions, not concrete implementations. They rely on interfaces or abstract classes that define the required functionalities.
  2. Low-level modules implement the abstractions. They provide specific implementations that adhere to the defined interfaces and fulfill the required functionalities.
  3. Clients (high-level modules) interact with the abstractions, not the concretions. They use the interfaces or abstract classes to access the desired functionalities without knowing the specific implementations.

Benefits

  • Loose coupling Changes in low-level modules (e.g., new materials or construction techniques) don't affect high-level modules as long as the abstractions remain the same.
  • Increased flexibility You can easily swap different implementations under the same abstraction, adapting the system to new needs without modifying the high-level code.
  • Improved testability Mocking and testing abstractions become easier, as you're not tied to specific concrete implementations.
  • Open-closed principle You can extend functionality by adding new implementations without modifying existing code.

Remember

  • Use DIP when your code has complex dependencies or needs to be flexible and adaptable.
  • Overuse can lead to unnecessary abstraction and complexity for simple tasks.
  • Choose clear and descriptive names for abstractions and implementations.