Skip to main content

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:

  1. find the seams (inputs/outputs of each responsibility)
  2. extract one responsibility into a collaborator
  3. inject it via constructor
  4. test the extracted class alone
  5. repeat until the original class only orchestrates

Each step keeps the program working โ€” refactoring is a series of small verified moves, not a rewrite.

Now practice

Architecture LabLayer a checkout flow end-to-end, extract a pricing collaborator, and prove injection with two clocks.3 challenges ยท ยท ~40 min