SOLID Principles • Dependency Inversion PrincipleMedium⏱️ ~2 min
DIP Structure: Abstraction Layer Design
The Dependency Inversion Principle creates a three-layer architecture where abstractions mediate between high-level policy and low-level details.
OrderProcessor
- repository: IOrderRepository
+ processOrder(order): Result
↓ depends on
«interface»
IOrderRepository
+ save(order): Boolean
+ findById(id): Order
+ findById(id): Order
△ implements
MySQLRepository
+ save(order): Boolean
+ findById(id): Order
+ findById(id): Order
MongoRepository
+ save(order): Boolean
+ findById(id): Order
+ findById(id): Order
Key Structural Elements:
First, the high-level module (
Second, the abstraction layer (
Third, low-level modules (
Fourth, the dependency direction flows upward from concrete implementations to abstractions, inverting the traditional top-down dependency flow.
OrderProcessor) contains business logic and owns the abstraction interface definition. It declares what operations it needs through IOrderRepository.Second, the abstraction layer (
IOrderRepository) defines the contract using domain language meaningful to the high-level module. Method names reflect business operations, not technical implementation details.Third, low-level modules (
MySQLRepository, MongoRepository) implement the abstraction. They depend on the interface definition, not the other way around.Fourth, the dependency direction flows upward from concrete implementations to abstractions, inverting the traditional top-down dependency flow.
Interview Tip: Clarify that the interface belongs to the high-level module's package or namespace, not the low-level module's. This ownership inversion is critical to achieving proper decoupling.
Abstraction Ownership: The interface should be defined where it is used (high-level module), not where it is implemented (low-level module). This ensures that changes to low-level implementations do not force changes to the abstraction that high-level modules depend on.
💡 Key Takeaways
✓High-level modules define abstractions based on their needs, not implementation capabilities
✓Interfaces use domain language from the perspective of the consumer
✓Low-level modules implement abstractions defined by high-level modules
✓Dependency arrows point from concrete classes toward interfaces
✓The abstraction layer creates a stable boundary between policy and details
📌 Examples
1Business logic layer defines repository interfaces that database layer implements
2Service layer defines gateway interfaces that external API clients implement
3Controller defines use case interfaces that application services implement