Mansoor ul Haq
Mansoor ul Haq

Reputation: 35

Dependency Inversion Principle's second statement elaboration

Following two statements are core of the Dependency Inversion Principle(DIP):

"High-level modules should not depend on low-level modules. Both should depend on abstractions." "Abstractions should not depend on details. Details should depend on abstractions."

I read different books and article about DIP; all of them explained the first statement but none of them explain the second statement: "Abstractions should not depend on details. Details should depend on abstractions". Please explain what exactly are the meaning of this second statement.

Upvotes: 2

Views: 1053

Answers (3)

Ravindra babu
Ravindra babu

Reputation: 38950

Don't decide on interface (abstraction) by looking at implementation (details) first.

e.g. You can define a Repository interface. But while designing Repository interface, you should not decide on interface (abstraction) by looking at specific solutions like SQL implementation or NoSQL implementation ( details).

Let the Repository interface is generic and SQL features Or NoSQL feature implementation should be specific.

You will get clarity about the second statement if you read this article by Martin Fowler

Switch out the repository for a different storage mechanism, there's no mention of SQL in its interface so we can use an in-memory solution, a NoSql solution or a RESTful service.

Upvotes: 2

Frank Puffer
Frank Puffer

Reputation: 8215

It just means that you don't want to change an abstraction just because a detail has changed because details are likely to change.

Because both high-level and low-level modules depend on abstractions, they will also have to be changed whenever a detail has changed. This would obviously be undesirable.

Upvotes: 2

Benjamin Caure
Benjamin Caure

Reputation: 2403

You should think of "details" as "implementations": - if you declare some interface, it does not depend on its future implementation classes. - in the other hand, implementation classes should reference their interface and implement its methods, so they depends on it.

Upvotes: 1

Related Questions