Singleton

Intent: Ensure a class has only one instance and provide a global access point to it.

When to use it

  • Exactly one shared resource is needed (config, logger, connection pool)
  • Lazy initialisation of an expensive object

Example

An app-wide settings store that every screen reads.

Code sketch (TypeScript)

class Config {
  static #instance: Config;
  static get() { return (this.#instance ??= new Config()); }
  private constructor() {}
}

Learn patterns step by step in the Learn area →