Skip to content

Creational Patterns — Detailed#

Singleton#

Single instance, accessed via a static method.

classDiagram
  class Singleton {
    -instance: Singleton
    -Singleton()
    +getInstance() Singleton
    +operation()
  }
  Singleton --> Singleton : returns self
class Logger {
  private static volatile Logger INSTANCE;
  private Logger() {}
  public static Logger getInstance() {
    if (INSTANCE == null) synchronized (Logger.class) {
      if (INSTANCE == null) INSTANCE = new Logger();
    }
    return INSTANCE;
  }
}

Watch: hidden global state, breaks testability. Most languages prefer a single instance passed via DI rather than a true singleton.


Factory Method#

A method on a base class decides which concrete to instantiate.

classDiagram
  class Creator {
    <<abstract>>
    +createProduct() Product
    +operation()
  }
  class ConcreteCreatorA {
    +createProduct() Product
  }
  class ConcreteCreatorB {
    +createProduct() Product
  }
  class Product
  class ConcreteProductA
  class ConcreteProductB
  Creator <|-- ConcreteCreatorA
  Creator <|-- ConcreteCreatorB
  Product <|-- ConcreteProductA
  Product <|-- ConcreteProductB
  ConcreteCreatorA ..> ConcreteProductA
  ConcreteCreatorB ..> ConcreteProductB

Use when the caller shouldn't know which concrete type comes back.


Abstract Factory#

A factory that returns families of related products (e.g. a UI toolkit's Button + Checkbox + Menu).

classDiagram
  class GUIFactory {
    <<interface>>
    +createButton() Button
    +createCheckbox() Checkbox
  }
  class MacFactory
  class WinFactory
  GUIFactory <|-- MacFactory
  GUIFactory <|-- WinFactory
  class Button
  class Checkbox
  class MacButton
  class WinButton
  Button <|-- MacButton
  Button <|-- WinButton

Builder#

Construct a complex object step-by-step. Most useful when an object has many optional parameters.

Pizza p = new Pizza.Builder()
    .size(LARGE)
    .addTopping("cheese")
    .addTopping("olives")
    .crust("thin")
    .build();

Java records / Kotlin data classes / Python kwargs replace some of this, but the fluent-builder is still common.


Prototype#

Clone an existing instance instead of constructing a new one. Useful when construction is expensive.

classDiagram
  class Prototype {
    <<interface>>
    +clone() Prototype
  }
  class Page {
    -title
    -content
    +clone() Page
  }
  Prototype <|.. Page

Choosing among them#

Use Pick
Hide one type's construction Factory Method
Hide a family of types together Abstract Factory
Many optional / required fields Builder
Construction is expensive; you want copies Prototype
You truly need exactly one instance Singleton (sparingly)

Where these show up in this site#

  • Logger framework uses Singleton for the root logger + Factory for appenders.
  • Vending machine / ATM / Hotel management use Builder for complex requests.
  • Chess engine uses Prototype for memento/undo snapshots.

Glossary & fundamentals#

Concepts referenced in this design. Each row links to its canonical page; the tag column shows whether it is a high-level (HLD) or low-level (LLD) concept.

Tag Concept What it is Page
HLD Observability metrics, logs, traces, SLOs observability
LLD Creational patterns Singleton, Factory, Builder, Prototype creational-patterns
LLD Behavioural patterns Strategy, Observer, State, Command, Chain behavioral-patterns
LLD Dependency injection DI / IoC, constructor injection, containers dependency-injection