Dependency Inversion Principle – Understanding Low and High Level Components

designdesign-principlesobject-oriented

I'm learning about the Dependency Inversion Principle. It states that:

High level modules should not depend upon low-level modules. Both
should depend upon abstractions.

For a while I tried to understand what it means that both the high level components and the low level components, rely on the abstractions and are dependent on them.

I'm assuming both should depend on the same abstraction in some way. Please correct me if this is wrong.

I have come to some conclusion about what this means. Please confirm if this is accurate.

enter image description here

"The high level components are dependent on the abstraction" – Meaning:

The high level components talk to an interface to communicate with the low level components, instead of communicating with concrete low level components directly. The low level components implement this interface.

"The low level components are dependent on the abstraction" – Meaning:

The low level components are defined and designed in the terms of the interface. They are designed to fit the interface. They are dependent on the interface, in the way that the interface defines how they are designed. (Often low level classes implement that interface).

This way, both the high level components and the low level ones are 'dependent on the abstraction', but in different ways.

Is this a good understanding?

Best Answer

Your understanding of the concept is very accurate.

Pointing out exceptions, special cases or philosophical minutiae right now would be diverting you from your present clarity of concept.

I would suggest, though, you use UML symbols:

enter image description here

  • Open arrow : uses
  • Closed arrow: inherits or implements
  • << name in brackets >> : interface or abstract class
Related Topic