01
Creational Patterns
Separating the knowledge of how an object gets built from the code that uses it: defining the pattern concept with its problem–solution–consequences triad, abstracting creation with the factory method and the abstract factory, sequencing a multi-parameter setup step by step, creating by copying, critiquing the singleton object in terms of global state, and moving the creation responsibility outside with dependency injection.
- 01 Pattern Concept Defining a pattern as the triad of problem, solution, and consequences: applying a registry solution already familiar from two separate lessons to carrier selection in the shipping library, measuring the two versions that produce the same output by file count, import edge count, the number of spots where a type name appears, and the longest import path, and counting the gain and the cost separately.
- 02 Factory Method and Abstract Factory Pulling object creation into a method and a family producer: the factory method, where the subtype decides which tariff to build; the abstract factory, which produces the matching triad of tariff, route, and label together; counting the wrong combinations silently accepted at the call site in the matching version; and comparing the file count edited when a new carrier family is added.
- 03 Builder Building a multi-field object step by step: comparing an eight-positional-parameter call against a builder made of named steps under the same mutation set, counting how many swapped arguments and missing fields each design silently accepts, the drop in the highest parameter count passed, and the pattern's cost in lines.
- 04 Prototype Creating an object by copying an existing one instead of constructing it: comparing a version that hand-writes derived tariffs' fields against one that clones a prototype, measuring the missing-field count when a field is added to the prototype, counting the child objects a shallow copy shares, and the cost of the derivation chain returning as a silent change.
- 05 Singleton Measuring, in terms of testability, holding a single instance as global state: applying the same four tests to a module-level single-instance registry and an instance-producing registry, counting the failed test count, whether the result changes when the test order changes, and the number of names the reset mechanism adds to the exposed surface.
- 06 Dependency Injection Moving the creation responsibility from the using code to the composition root: comparing a fee module that builds its own dependencies against a version that takes them at call time by direct dependency count and import closure size, counting the file edited when the discount policy changes, and showing that the closure does not disappear but moves.