Wiring & Refactoring
intermediate14 min readLesson 108 of 180
The composition root without a framework, and the five-step seam-extraction sequence for god classes.
Wiring dependencies by hand
Frameworks (Spring) do dependency injection for you; intermediates learn to wire by hand first โ it demystifies the container:
public static void main(String[] args) {
ExpenseRepository repo = new InMemoryExpenseRepository();
ReportService reports = new ReportService(repo);
CliController cli = new CliController(reports);
cli.run(); // the composition root โ the ONLY place that knows everything
}
The composition root is the single place where concrete classes meet. Everything below it sees interfaces only.
Refactoring a god class, in order:
- find the seams (inputs/outputs of each responsibility)
- extract one responsibility into a collaborator
- inject it via constructor
- test the extracted class alone
- repeat until the original class only orchestrates
Each step keeps the program working โ refactoring is a series of small verified moves, not a rewrite.