skipnothing/Design Patterns
UNIT 03

Creational Patterns

Factory, Builder, Singleton, patterns for object creation

Sometimes `new` isn't enough. Creational patterns give you control over how objects are created, composed, and represented.

Loading…
TOPICS
#12

Factory Method

Move the `new` behind one overridable method, so a subclass decides which class gets built and callers depend only on the shared type.

A `planRoute` method opens with `new Truck()`, and so does the mobile endpoint, the admin re-run button, and the retry job. The day sea freight arrives, every one of those files has to learn the word `Ship`.

12 min
#13

Abstract Factory

Group a family of related products behind one factory object, so a single startup choice swaps the whole set and a mismatched combination can never compile.

Your toolbar builds each widget on its own line: `new WinButton()` here, a checkbox three files over, a menu in the render loop. Ship the macOS skin and one of those lines keeps making the Windows part.

12 min
#14

Builder

Replace a positional constructor with an object that collects named steps and a closing build() call, so every value is labeled where you read it, validation runs before the object exists, and the same step sequence can assemble different products.

A constructor with six arguments looks fine until you read `new Coffee(12, true, false, 2)` and cannot tell which value is the milk. Swap the two booleans and it still compiles, runs, and quietly brews the wrong drink.

12 min
#15

Singleton

Separate the two promises Singleton bundles, one instance and a global access point, so you keep the single object but hand it in through constructors instead of a static getter that nothing declares.

You call `Config.getInstance()` in the order service, again in the report job, again in three more classes. None of their constructors mention config, yet every one of them quietly depends on the same hidden object.

10 min
UNLOCKS
All units