SOLID: Dependency Inversion Principle

Get familiar with the concept of the Dependency Inversion Principle and its example.

Introduction

The Dependency Inversion Principle (DIP) states that high-level modules should not depend on low-level modules, but rather both should depend on abstractions. The abstractions should not depend on details. Instead, the details should depend on abstractions.

In many cases, thinking about the interaction between modules as an abstract concept allows the linking of components to be reduced without the need for more coding patterns to be implemented. This allows for a functional scheme with reduced implementation and allows the system to be more flexible.

Real-life example

Let’s try to understand the concept of DIP with the help of a school example. Suppose there is a headmaster of a high school. Under the headmaster, there are faculty members such as teachers, assistants, and some helpers.

Violation

Let’s see what a possible design would look like without the implementation of the DIP.

The class diagram of this example is shown below:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.