A coach who builds a game plan around specific players must rewrite it whenever the lineup changes. A plan built around roles can accommodate different players who fulfill those roles. Dependency inversion applies the same idea to software: high-level modules and low-level modules both depend on abstractions, rather than high-level logic depending directly on concrete details. section 3.4.5
The abstraction defines a shared contract; concrete implementations fulfill it. The contract should not depend on those implementation details. This keeps business rules or orchestration loosely coupled to details such as database access, making changes and isolated tests easier. section 3.4.5
Listing 3.11 “Before”: Violating DIP
// Low-level modules (concrete players)
class Forward {
attack() { console.log(“Forward is attacking.”); }
}
class Midfielder {
controlMidfield() {
console.log(“Midfielder controlling the game.”);
}
}
// High-level module (the Coach)
class Coach { #A
constructor() {
this.forward = new Forward(); #B
this.midfielder = new Midfielder();#B
}
executeGamePlan() {
this.forward.attack();
this.midfielder.controlMidfield();
}
}Before inversion, the coach constructs specific players and calls their individual methods. section 3.4.5
Listing 3.12 “After”: Following DIP
// Low-level modules (depend on the abstraction)
class Forward {
performAction() {
console.log(“Forward is attacking.”);
}
}
class Midfielder {
performAction() {
console.log(“Midfielder is controlling the game.”);
}
}
class Winger { #A
performAction() {
console.log(“Winger is running down the sideline.”); #A
}
} #A
// High-level module (also depends on abstractions)
class Coach {
constructor(players) { this.team = players; } #B
executeGamePlan() {
for (const player of this.team) {
player.performAction();
}
}
}After inversion, the coach receives players and uses their shared performAction contract. A new player can join without changing the game plan. section 3.4.5
Injecting a logger
An order service that calls a static file logger is tied to that logging implementation. Instead, give the service a logger through its constructor and have it use the logging contract. A file logger can later be replaced with a cloud logger where services are registered, while a test can supply a fake logger without writing a file. Dependency injection supplies the dependency; the shared abstraction is what lets the service avoid depending on its concrete details. section 4.5.2 section 4.5.3
The tradeoff is a little setup complexity: a contract and a way to supply an implementation. The payoff is less coupling between business logic and technical concerns. At architectural scale, ports and adapters apply the same idea to protect core logic from dependencies on databases and other external details. section 4.5.3 section 5.1
Watch for concrete dependencies hidden inside important workflows. A checkout flow that directly calls a particular HTTP library and URL is hard to replace or mock. Inverting that dependency helps separate the external service from business logic, but network failures and timeouts still need explicit attention. section 10.4.1







