The Dependency Flow vs Control Flow
You'll learn: How dependency inversion changes which direction your code dependencies point, while runtime execution still flows in the original direction.
The Two Flows in Your Code
When you write software, two important flows exist:
Control flow is the path your program takes when it runs—which function calls which, where data moves at runtime. This is the story of what happens when you press Run.
Dependency flow is about which modules or files know about (depend on) other modules at compile-time. If Class A imports or references Class B, A depends on B. This is the story of what needs what to even compile.
The Magic of Inversion
Before dependency inversion, these two flows pointed the same direction:
- High-level code calls low-level code (control flow)
- High-level code imports/depends on low-level code (dependency flow)
After applying DIP, something remarkable happens:
- High-level code still calls low-level code at runtime (control flow unchanged)
- BUT high-level code now depends only on abstractions it owns (dependency flow reversed!)
- Low-level code depends on those abstractions (implements the interface)
Think of it like a restaurant: The chef (high-level) needs ingredients delivered (control flow happens), but instead of the chef depending on a specific supplier, the chef defines "I need fresh vegetables" (an interface). Suppliers adapt to that requirement. The vegetables still flow to the chef at dinner time, but the dependency points from supplier to chef's standard.
Key Takeaway:
Dependency Inversion separates compile-time dependencies from runtime control flow—your code calls downward at runtime, but dependencies point upward toward stable abstractions.