Home > Articles > Software Development & Management

  • Print
  • + Share This
Like this article? We recommend

Dependency of Inversion Principle

Probably the hardest of all the principles to understand is the Dependency of Inversion (DI) Principle. In simplest terms, I like to think of it as the solution to the OC Principle.

In short, the DI Principle says that the behavior of derived classes should not affect or influence the behavior of the base class from which they are derived. You know your code is in violation of this principle if it is also in violation of the OC Principle. Removing this violation requires the need for some sort of dependency injection.

The problem and solutions were illustrated previously with the OC Principle examples. In those examples, we had a high-level class called TextWriter that implemented a set of low-level classes based on the type of textwriter that was needed.

To introduce a new type of writer, we needed another low-level class (i.e., a class to write a text message to a database). This involved a change to the high-level class to support it or become aware that it existed.

The DI Principle says that the high-level class, TextWriter, should not need to be aware or have its behavior changed based on a low-level class. To remove this awareness or impact on the high-level class, we can use an interface that both the high-level classes and low-level classes share and depend on, instead of depending on each other.

  • + Share This
  • 🔖 Save To Your Account