2.2.10 · D1Design Principles

Foundations — Design Patterns — Creational - Singleton, Factory Method, Abstract Factory, Builder, Prototype

1,983 words9 min readBack to topic

Before you touch the parent topic, you must be able to read its vocabulary fluently. This page names every single word and symbol the parent leans on, defines it in plain language, ties it to a picture, and says why the topic needs it. Nothing below assumes prior knowledge — we build from the very first brick.


0. What even is an "object"?

Figure — Design Patterns — Creational -  Singleton, Factory Method, Abstract Factory, Builder, Prototype

Look at the picture: the box labelled logger1 holds a field count = 3 and exposes a button log(). That single box is one object. The whole chapter is about who gets to make these boxes and how.


The picture: the class is the cookie-cutter; each cookie it stamps out is an object.

Why the topic needs it: patterns like Singleton talk about "one instance of a class"; Factory Method talks about "which class to instantiate". Class vs object is the split the whole chapter dances on.


2. new and the constructor — the birth event

Figure — Design Patterns — Creational -  Singleton, Factory Method, Abstract Factory, Builder, Prototype

In the figure, the red arrow is the new call: it takes the blueprint and stamps out a live box. Every time you write new, a brand-new box appears.


3. Fields and static — box memory vs blueprint memory

Figure — Design Patterns — Creational -  Singleton, Factory Method, Abstract Factory, Builder, Prototype

Look at the picture: count (non-static) sits inside each box, so box A and box B each have their own. But instance (static) floats next to the blueprint — there is exactly one of it. That single shared slot is precisely how Singleton stores its one-and-only object.

Why the topic needs it: the parent's Singleton code writes private static Logger instance;. Without knowing static = "one slot per class", that line is unreadable.


4. private and public — the access wall

The picture: think of a fence around the box. public methods are gates in the fence; private methods and the private constructor are inside, unreachable from outside.


5. Interface & abstract — a promise without a body

Figure — Design Patterns — Creational -  Singleton, Factory Method, Abstract Factory, Builder, Prototype

In the figure, Button is a hollow shape (contract only); WinButton and WebButton are solid shapes that fit into the same hollow socket. Code that only knows the socket shape can accept either solid piece.

Why the topic needs it: Factory Method puts an abstract createButton() in the base class; Abstract Factory is literally an interface listing several such methods. The word "interface" appears in almost every pattern definition.


6. Inheritance, extends, implements, override

Why the topic needs it: the parent says "subclasses decide which class to instantiate, by overriding a creation method." Override is the mechanism of Factory Method. Composition (holding another object as a field) vs inheritance is exactly the line the parent draws between Factory Method (subclass) and Abstract Factory (composition).


7. Reference vs copy — the cloning trap

Figure — Design Patterns — Creational -  Singleton, Factory Method, Abstract Factory, Builder, Prototype

Left of the figure: shallow copy — the clone gets its own outer box but its inner arrow points at the same nested list as the original. Change the list through one, and the other sees it too. Right: deep copy — the nested list is also duplicated, so the two are fully independent.

Why the topic needs it: Prototype clones objects. The parent's biggest warning ("shallow ≠ deep") is invisible unless you can picture two arrows landing on one shared box. Hold this picture.


8. Immutability & invariants — why Builder ends with build()

Why the topic needs it: Builder's final build() call is where it validates every invariant and hands back a frozen (immutable) object — "no half-built objects leak out." Without "immutable" and "invariant", build() looks pointless.


Prerequisite map

object = data plus actions

class = blueprint

new plus constructor = birth

static field = one shared slot

private plus public = access wall

Singleton

interface and abstract = contract

extends implements override

Factory Method

Abstract Factory

Builder needs immutable and invariant

reference vs copy

Prototype

immutable object

Read top to bottom: everything grows from object and class. The three shaded destinations (Singleton, Factory Method, Prototype…) are the parent's patterns — each one is fed by exactly the foundations above it.


Equipment checklist

Cover the right side and answer aloud. If any line stumps you, re-read its section.

What is the difference between a class and an object?
A class is the blueprint (cookie-cutter); an object is one live box stamped from it. One class → many objects.
What does the keyword new do, and why is it the villain the chapter fights?
It calls a constructor to build a fresh object of a named concrete class — scattering new ConcreteX() hard-wires your code to that class.
What does static mean for a field?
The field belongs to the class itself, so there is exactly ONE shared slot for all objects — this is how Singleton stores its single instance.
Why does Singleton make its constructor private?
To lock the birth-door so no outsider can call new; the only way in is the one public static access method.
What is an interface, and how does it differ from a class?
A list of method names with no bodies — a contract. A class supplies the actual code; an interface only promises the shape.
What is the difference between extends and implements?
extends inherits from a class; implements fulfils an interface contract. Both let a subclass override method bodies.
What does it mean to override a method, and which pattern relies on it?
To replace a parent's method with your own same-named version — Factory Method uses override so subclasses decide which class to instantiate.
Two variables point at the same box — what is that arrow called, and why does it matter for Prototype?
A reference. A shallow clone copies the reference (shared nested box); a deep clone duplicates the nested box too.
What is an invariant, and where does Builder check it?
A rule that must always hold for a valid object; Builder validates all invariants inside build() before returning the finished immutable object.

Now proceed to the parent topic — you have every symbol it uses. Related deeper reading: Design Principles, Dependency Injection, Structural Patterns, Behavioral Patterns.