I am currently reading an otherwise excellent tutorial regarding the Dependency Inversion Principle
https://www.baeldung.com/java-dependency-inversion-principle
and there is something I am not able to interpret despite considerable long time of thinking
The relevant part from definition of DIP: "High-level modules should not depend on low-level modules. Both should depend on abstractions."
In point 3.1 "Design Choices and the DIP" the author introduces the principle through an example where a StringProcessor
class uses a StringReader
and a StringWriter
component and gives multiple design choices wrt using interfaces/classes and packages. My problem is with choice 2 which is
"StringReader
and StringWriter
are interfaces placed in the same package along with the implementations. StringProcessor
now depends on abstractions, but the low-level components don't. We have not achieved inversion of dependencies yet"
StringProcessor
is the "high level component" which depends on the "abstractions" i.e. StringReader
and StringWriter
interfaces, thereby fulfilling DIP definition from one side, that is clear. Now given the terminology used throughout the article "the implementations" referred in the first sentence, e.g. a ConcreteStringReader
and a ConcreteStringWriter
class would be the "low level components" here and what I am just not able to understand how they do not depend on the "abstractions" i.e. on the interfaces they implement, regardless of the containing packages
Obviously putting implementations in the same package with their interfaces might not be the best from code organization point of view, but how this violates the verbatim DIP definition above wrt depending on abstractions is currently beyond my understanding
Perhaps someone with a deeper theoretical knowledge on the topic might help me out here
question from:
https://stackoverflow.com/questions/65541236/dependency-inversion-design-choice-question 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…