Dependency Inversion — Depend on abstractions, not concretions
High-level code depends on interfaces; wiring happens at the edge. · Constructor injection makes tests use mocks easily.
Watch
Watch, then scroll down for code and practice.
In code
interface UserRepo {
findById(id: string): Promise<User | null>;
}
class UserService {
constructor(private repo: UserRepo) {}
async getName(id: string) {
const u = await this.repo.findById(id);
return u?.name ?? "unknown";
}
}📘 Key ideas
The principle
High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details.
The smell
UserService does this.db = new MySQLDatabase() in its constructor. Switching to Postgres requires editing UserService.
Constructor injection
Pass the IUserRepository interface into UserService's constructor. The caller decides which implementation to inject. UserService never imports a concrete DB class.
Benefit: testability
Inject a MockUserRepository in tests. No real database needed. Tests run fast and in isolation.
🧠 Practice — Apply What You Learned
SRP: Fix the Overloaded Logger
You are given a Logger class that does too much: it formats log messages, filters by level…
OCP: Extend Discounts Without Modifying
A PriceCalculator class uses a long if-else chain to apply discounts: if user is Student, …
LSP: The Square–Rectangle Problem
The classic LSP violation: Square extends Rectangle. Since a square's width and height mus…
ISP: Break the Fat Worker Interface
A Worker interface has four methods: work(), eat(), sleep(), requestLeave(). HumanWorker i…
DIP: Decouple the Database Layer
UserService directly instantiates MySQLDatabase inside its constructor: `this.db = new MyS…
Logger / Logging Framework
Design a flexible logging framework that supports multiple log levels, formatters, and out…
Payment Gateway
Design a payment processing system that handles transactions across multiple payment metho…
🚀 Now apply what you learned
Pick a problem above, write your solution, and get AI feedback on your design.
Start Practice →