avatarAli Zeynalli

Summary

This article discusses the Dependency Inversion Principle (DIP) as part of the SOLID software design principles, focusing on its application in a Spring Boot Application.

Abstract

The article is the fifth and final part of a blog series that explores the SOLID software design principles, specifically focusing on the Dependency Inversion Principle (DIP). The author explains the theory behind DIP, emphasizing the importance of achieving maximum flexibility in a software system by ensuring that high-level modules are independent of low-level module implementation details. The author also clarifies the difference between DIP and Inversion of Control (IoC), stating that IoC is more framework-level, while DIP should be a part of everyday programming activities. The article concludes by providing an example of DIP implementation in a cash flow management application.

Opinions

  • The author believes that the ideal world for a software system is one with maximum flexibility.
  • The author suggests that the Abstract Factory Design Pattern is a good solution for implementing DIP.
  • The author advises against referring to, deriving from, or overriding volatile concrete classes.
  • The author acknowledges that it is not possible to build a software system completely on abstractions and that the goal should not be to avoid concrete imports completely but to be careful with the volatility of classes when building object-oriented design relationships.
  • The author provides an example of DIP implementation in a cash flow management application, highlighting the use of Converter, Calculator, and Repository Service Interfaces.
  • The author encourages readers to connect with them on Twitter or LinkedIn.
  • The author promotes an AI service that they recommend, which provides the same performance and functions as ChatGPT Plus (GPT-4) but is more cost-effective.

How to apply SOLID Software Design Principles to Spring Boot Application (Part 5)

DIP: Dependency Inversion Principle

Photo by Tim Mossholder on Unsplash

(skip the first paragraph if you have already read other parts of this blog series)

This article is the last part of the blog series, dedicated to well-known software design principles, that evolved over time and were finally summarised by Robert C. Martin with initials of the corresponding principles. These principles guide us how to build well-designed software systems, giving best practices for arranging classes, functions, building blocks. We will look at each principle in depth and apply it to the Spring Boot Application. The idea is refactoring existing software based on these principles from architectural point of view.

The word S.O.L.I.D. stands for:

  • SRP: Single Responsibility Principle
  • OCP: Open-Closed Principle
  • LSP: Liskov Substitution Principle
  • ISP: Interface Segregation Principle
  • DIP: Dependency Inversion Principle

This fifth part is about DIP. Before jumping to concrete example, let’s have some theory.

Dependency inversion principle is when dependency modules are reversed, thus rendering high-level modules independent of the low-level module implementation details.

For a software system, ideal world is when a maximum flexibility is achieved. DIP states exactly this: source code dependencies should import only abstractions. High-Level components should connect to Low-Level components, preferably through abstractions. And abstractions should not depend on details, details should depend on abstractions.

It should also not be forgotten that there is a fundamental difference between Dependency Inversion Principle and Inversion of Control(IoC), although the basic idea is the same. IoC is engaged more in framework level (For instance, how Spring Boot handles bean initialisation), whereas DIP should be part of programmers’ everyday activity.

Some important advises from R.C.Martin concerning Dependency Inversion Principle are following:

  1. Don’t refer to volatile concrete classes
  2. Don’t derive from volatile concrete classes
  3. Don’t override concrete functions
  4. Never mention the name of anything concrete and volatile (a little bit utopic)

Good solution for DIP might be Abstract Factory Design Pattern. Please have a look at Design Patterns for more Information.

But it is not possible to build a software system completely on abstractions. There are and there will be definitely classes with concrete imports. The goal should not be to avoid it completely, but always being careful with volatility of classes when building object oriented design relationships.

In a practical part, we are not going to extend our cash flow management application, since we already implemented DIP automatically at several places.

Class Diagram of cashflow.core.* package

Above is the final version of Class Diagram for cashflow.core.* packages. By taking closer glance at class structure, the one might see that Dependency Inversion Principle is already applied at Converter, Calculator and Repository Service Interfaces.

For more in-depth understanding please have a look at corresponding git branch.

This was the fifth and last part of SOLID Principles.

P.S. You can connect with me on twitter or linkedin.

Spring Boot
Clean Code
Solid Principles
Software Development
Software Architecture
Recommended from ReadMedium