Creational
How objects are created, hiding instantiation logic.
Creational patterns abstract the instantiation process. Instead of having
new Foo() scattered throughout the code, they centralize the decision of what to create
and how to create it, allowing the client to work with interfaces or abstract types.
#Why they matter
In a small system, new is fine. But as it grows, direct new calls
end up coupling consumers to concrete classes, hiding dependencies and
making testing painful. Creational patterns move that decision to a
single, replaceable location.
#When they fit
- Creation requires multiple steps, validations, or external configuration.
- You want client code to depend on abstractions, not concrete types.
- The family of objects to be instantiated may vary (by environment, by tenant, by feature flag).
- You need to control the lifecycle (single instance, pool, clonable prototypes).
#When NOT to use them
- If you only have one class and there will never be another, a factory is unnecessary ceremony.
- Singleton in particular is overused: often what you want is to inject a dependency, not globalize it.
#In this chapter
- Singleton — a single shared instance (with its thread-safe variants).
- Factory Method — delegate creation to subclasses.
- Abstract Factory — families of related objects.
- Builder — step-by-step construction of complex objects (with Director and Step Builder).
- Prototype — clone instead of building from scratch.
Tip: If you're unsure between Factory Method and Abstract Factory, start with the former. Upgrading to Abstract Factory is trivial; downgrading is painful.
- Singleton — Ensures a class has only one instance and provides a global access point to it.
- Factory Method — Defines an interface for creating an object, but lets subclasses decide which class to instantiate.
- Abstract Factory — Creates families of related objects without specifying their concrete classes.
- Builder — Constructs complex objects step by step, separating the construction algorithm from its representation.
- Prototype — Creates new objects by cloning an existing instance, avoiding reliance on its concrete classes.