Implementing DIP for Enhanced Flexibility
Understand how to implement DIP for writing code that depends on abstraction, enabling flexibility and decoupling in software design.
In this lesson, we’ll learn how the DIP allows us to split code into separate components that can change independently of each other. We’ll then see how this naturally leads to the OCP part of SOLID.
Coding for abstraction, not details
Dependency inversion (DI) means that we write code to depend on abstractions, not details.
The opposite of this is having two code blocks, one that depends on the detailed implementation of the other. Changes to one block will cause changes to another. To see what this problem looks like in practice, let’s review a counter-example. The following code snippet begins where we left off with the Shapes
class after applying SRP to it:
Get hands-on with 1400+ tech skills courses.