100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Singleton Pattern

BeginnerTechnique9.6K learners

The Singleton pattern is a creational design pattern that restricts a class to a single instance and provides a global point of access to that instance.

Definition

The Singleton pattern is a creational design pattern that restricts a class to a single instance and provides a global point of access to that instance.

Overview

The Singleton pattern is one of the original 23 design patterns catalogued by the 'Gang of Four' (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) in their 1994 book Design Patterns. It solves a specific problem: ensuring that a class has exactly one instance throughout the lifetime of a program, and giving global, controlled access to that instance, rather than letting arbitrary code create competing copies. Typical implementations make the class's constructor private or protected, store the single instance in a static field, and expose a static accessor method (often named `getInstance()`) that creates the instance on first use and returns the cached instance on subsequent calls. Common legitimate uses include managing shared resources where having more than one instance would be wasteful or incorrect — a single connection pool, a single logging facility, a single configuration object, or a single hardware-interface wrapper. In multi-threaded environments, care must be taken to make instance creation thread-safe, typically via double-checked locking, an eagerly initialized static field, or language-specific idioms (such as Python's module system, which is naturally singleton-like, or enum-based Singletons in Java). Despite its ubiquity, the Singleton pattern is also one of the most criticized patterns in modern software design. Critics point out that it introduces global mutable state, creates hidden dependencies between classes (since any code can silently call the accessor rather than declaring the dependency explicitly), and makes unit testing significantly harder because the singleton's state persists across tests and cannot easily be swapped for a mock or stub. Many modern codebases favor dependency injection instead: rather than a class reaching out to a global singleton, the single shared instance is constructed once at the application's composition root and explicitly passed into the classes that need it, preserving the 'only one instance' property while keeping dependencies visible and testable.

Key Concepts

  • Guarantees a class has exactly one instance for the application's lifetime
  • Provides a single, well-known global access point to that instance
  • Typically implemented with a private constructor and a static accessor method
  • Instance can be created eagerly (at class load) or lazily (on first access)
  • Requires careful handling for thread-safety in concurrent environments
  • Often used for shared resources: configuration, logging, connection pools, caches
  • Frequently criticized for introducing global state and hidden dependencies
  • Commonly replaced in modern code by dependency injection of a single shared instance

Use Cases

Centralized application configuration objects
Shared logging facilities
Database connection pools or caches meant to be shared application-wide
Hardware or driver interfaces where only one instance can safely exist
Registries or service locators in smaller applications
Managing a single instance of an expensive-to-create resource

Frequently Asked Questions