skip to content

Creational Patterns

Patterns that separate how an object is created from how it is used, so a system does not hard-code its own construction: Singleton, Factory Method, Abstract Factory, Builder, Prototype and Object Pool.

part ofSoftware design & architectureoverview, primer and where to startread it →
on this pageshow

questions

page 2 of 2

How do you implement a correct deep copy of an object graph that contains cycles and objects referenced from more than one place?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Carry a map from each already-copied original object to its copy. Before copying anything, look it up: if it is in the map, reuse that copy instead of recursing. This stops infinite loops on cycles and keeps shared objects shared in the copy.

open as a page

When cloning an object with the Prototype pattern, which parts of its state should NOT be copied verbatim, and what should happen to them instead?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Anything that identifies the object or ties it to the outside world: database IDs, creation timestamps, version counters, open connections and file handles, locks, caches and registered listeners. These should be reset, regenerated, or re-established rather than duplicated.

open as a page

"Exactly one instance" — one per what? What boundaries actually limit a Singleton's uniqueness guarantee, and what can silently break it?

level: seniorimportance: should knowfreq 45%

basics

~20 s

One per isolated runtime unit — normally one process (and, in some runtimes, one class loader or module instance). Run two processes, replicas, or workers and you have several. Reflection, deserialization, cloning, and code reloading can also create extras inside one process.

open as a page

How do you decide how much creational indirection a system actually needs — and what signals tell you a factory or builder layer should be removed rather than added?

level: principalimportance: should knowfreq 34%

basics

~20 s

Add creational indirection only where something genuinely varies — a second implementation, a lifetime rule, or complex assembly. If a factory has one implementation, its methods just forward to one constructor, and nothing is chosen at runtime, delete it and call the constructor.

open as a page

The Gang of Four classify Factory Method as a 'class creational' pattern and Abstract Factory, Builder, Prototype and Singleton as 'object creational'. What does that distinction mean, and why does it affect when the concrete class gets decided?

level: seniorimportance: nice to knowfreq 26%

basics

~20 s

Class creational means the variation comes from inheritance — a subclass overrides a creation method, so the choice is fixed when you pick the subclass. Object creational means creation is delegated to another object, so you can change it at runtime by swapping that object.

open as a page

Abstract Factory is said to enforce consistency within a product family. What exactly does that guarantee cover, and what are its limits?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Because one factory object produces every part, code that uses that factory gets parts from a single variant. The guarantee is only as strong as the discipline of always going through the factory — direct construction, caching, serialization, or two factories in scope can still mix variants.

open as a page

When is the Builder pattern the wrong choice, and what alternatives deliver the same benefits with less machinery?

level: principalimportance: nice to knowfreq 24%

basics

~20 s

Skip Builder when a class has few, mostly required fields, or when the language already has named arguments with defaults. Builders duplicate the field list, hide missing values until runtime, and a reused builder can leak state from one product into the next.

open as a page

How does polymorphic creation scale to a plugin or extension architecture, and what changes when the concrete implementations are chosen at runtime by configuration or third-party code?

level: principalimportance: nice to knowfreq 26%

basics

~20 s

Subclass-based creation can't pick an implementation from a config string, so systems move to a registry: a map from key to creator, filled at startup by wiring or plugin discovery. Extension becomes registration, and errors move from compile time to runtime.

open as a page

Beyond a maximum size, what policies shape an Object Pool's behaviour — idle ordering (LIFO vs FIFO), minimum idle and warm-up, max lifetime, and validation — and what does each trade off?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Besides the maximum, a pool decides which idle object to hand out (most recently returned, or least recently), how many to keep warm and pre-create at startup, how long an object may live before being retired, and whether to health-check it before lending it out.

open as a page

Serializing an object and immediately deserializing it is a popular way to get a deep copy. What are the trade-offs of that technique, and what would you prefer at scale?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Serialize-then-deserialize gives a deep copy in one line and handles nested structures generically. But it is slow, copies everything including things you should not copy, silently drops fields the format cannot represent, and can lose shared references or fail on cycles.

open as a page

You inherit a large codebase where dozens of classes call static Singleton accessors, and the test suite is slow and order-dependent as a result. How do you plan and sequence the removal of that coupling without a big-bang rewrite?

level: principalimportance: nice to knowfreq 28%

basics

~20 s

Don't rewrite everything. Give each singleton an interface and instance methods, keep the static accessor as a thin wrapper that delegates to one injected instance, then move call sites to constructor injection module by module, starting with the ones causing test pain. Delete the accessor last.

open as a page

showing 31–41 of 41