Dependency Injection as a Creational Technique
Learn how dependency injection complements creational patterns by moving object construction out of consumers and into a central composition point.
Why Another Creational Approach?
Classic creational patterns like Factory and Builder centralize how objects are built. Dependency Injection (DI) centralizes where they are wired together.
- A class no longer creates its own collaborators.
- Instead, collaborators are supplied from outside.
This is the natural endpoint of the creational journey: removing the new keyword from business logic entirely.
The Problem DI Solves
Consider a service that builds its own dependency:
The service is now hard-wired to a concrete class. You cannot swap it for a test double or an alternative implementation without editing the service.
class ReportService {
private final MySqlDatabase db = new MySqlDatabase();
void run() { db.query("..."); }
}All lessons in this course
- Singleton and Factory Method
- Abstract Factory and Builder
- Prototype and Object Pool
- Dependency Injection as a Creational Technique