IT lexicon › Programming › Singleton pattern

Singleton pattern

Programming På svenska → Updated: 2026-05-23

Gang-of-Four pattern — ensure a class has exactly one instance and give global access to it. Famously an anti-pattern as often as a pattern.

Classic Java: private constructor + static getInstance(). Thread safety requires double-checked locking or eager init. Modern variant: a dependency-injection container gives you "singleton scope" without global state.

Anti-pattern critique: makes testing hard (global state), breaks dependency injection, hides dependencies. Acceptable for truly process-global things (logger, cache). Prefer DI.

← Back to the lexicon