Do the "Dependency Inversion Principle" (DIP) and "Design to Interfaces Principle" express the same principle? If not, what would be the difference?
EDIT
To clarify and narrow down the context a bit: by interface I mean a programmatic interface, like a Java interface
or a pure abstract base class in C++. No other 'contracts' are involved.
Dependency inversion principle is one of the principles on which most of the design patterns are build upon. Dependency inversion talks about the coupling between the different classes or modules.
The Inversion of Control is a fundamental principle used by frameworks to invert the responsibilities of flow control in an application, while Dependency Injection is the pattern used to provide dependencies to an application's class.
Dependency Injection is an implementation of Dependency Inversion Principle. One of the ways to achieve Open-Close Principle is to use Dependency Inversion Principle. High-level modules in DIP are modules that appear on the higher part of the UML diagram and depends on the abstraction layer.
I just wanted to pitch in and quote Derek Greer on another question very similar to this one, since it does answer this question nicely, in my opinion.
"What the Dependency Inversion Principle does not refer to is the simple practice of abstracting dependencies through the use of interfaces (e.g.
MyService → [ILogger ⇐ Logger]
)."
While this decouples a component from the specific implementation detail of the dependency, it does not invert the relationship between the consumer and dependency (e.g. [MyService → IMyServiceLogger] ⇐ Logger
)."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With