VOOZH about

URL: https://www.geeksforgeeks.org/system-design/singleton-design-pattern/

⇱ Singleton Method Design Pattern - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Singleton Method Design Pattern

Last Updated : 5 May, 2026

The Singleton Design Pattern ensures that a class has only one instance and provides a global access point to it. It is used when we want centralized control of resources, such as managing database connections, configuration settings or logging.

  • Prevents accidental creation of multiple instances and ensures controlled, efficient use of resources like memory and connections.
  • Simplifies coordination across different parts of the application by providing a single shared instance.

Example: A Database Connection Manager uses a single shared instance instead of creating multiple connections. This ensures efficient resource usage and consistent access to the database across the application.

👁 client_1
Singleton Design Pattern

In the above Diagram:

  • All clients (Client-1, Client-2, Client-3) send requests but receive the same single instance (Instance-1), showing that only one object is created.
  • The Singleton object acts as a central access point, ensuring every request is handled by the same shared instance.
  • This avoids multiple object creation, leading to efficient resource usage and consistent behavior (e.g., one database connection manager).

Real-World Applications

The Singleton Pattern ensures a single, globally accessible instance of a class, providing controlled access and consistent behavior across an application.

  • Logging Systems : Maintain a consistent logging mechanism across an application.
  • Configuration Managers : Centralize access to configuration settings.
  • Database Connections : Manage a single point of database access.
  • Thread Pools : Efficiently manage a pool of threads for concurrent tasks.

Features

The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it, while efficiently managing resources.

  • Single Instance: Ensures only one object of the class exists in the JVM.
  • Global Access Point: Provides a centralized way to access the instance.
  • Lazy or Eager Initialization: An Instance can be created at class load time (eager) or when first needed (lazy).
  • Thread Safety: Can be designed to work correctly in multithreaded environments.
  • Resource Management: Useful for managing shared resources like configurations, logging or database connections.

Components

The main key components of Singleton Method Design Pattern are:

👁 stack_memory
Components

1. Static Member

The Singleton pattern employs a static member within the class. This static member ensures that memory is allocated only once, preserving the single instance of the Singleton class.

2. Private Constructor

The Singleton pattern incorporates a private constructor, which serves as a barricade against external attempts to create instances of the Singleton class. This ensures that the class has control over its instantiation process.

3. Static Factory Method

A crucial aspect of the Singleton pattern is the presence of a static factory method. This method acts as a gateway, providing a global point of access to the Singleton object. When someone requests an instance, this method either creates a new instance (if none exists) or returns the existing instance to the caller.

Thread-Safety in Singleton Design Pattern

In a multi-threaded environment, the basic implementation of the Singleton Design Pattern may fail due to a problem known as a race condition. When multiple threads try to access the Singleton instance at the same time, it can lead to the creation of more than one instance, which violates the core principle of Singleton.

👁 race_condition_in_singleton_thread_unsafe_

Explanation

To understand the issue, consider two threads executing simultaneously:

  • Step 1: Thread 1 checks whether the instance (obj) is null. Since no object has been created yet, the condition is true.
  • Step 2: Before Thread 1 creates the object, it gets paused (context switching happens).
  • Step 3: Thread 2 now runs and also checks obj == null. It also finds it true.
  • Step 4: Thread 2 creates a new Singleton object.
  • Step 5: Thread 1 resumes execution and, unaware that an object has already been created, also creates another instance.

Different Ways of Implementation

Sometimes we need to have only one instance of our class for example a single DB connection shared by multiple objects as creating a separate DB connection for every object may be costly. Similarly, there can be a single configuration manager or error manager in an application that handles all problems instead of creating multiple managers.

1. Classic (Lazy Initialization)

In this method, the Singleton instance is created only when it is first requested. This helps save memory and resources, but the basic implementation is not thread-safe.

Example: Classical Java implementation of singleton design pattern

  • getInstance() is declared static, so it can be called without creating an object of the class.
  • The first call to getInstance() creates the singleton object.
  • Subsequent calls return the same instance, ensuring only one object exists.

Note: Singleton obj is not created until we need it and call the getInstance() method. This is called lazy instantiation. The main problem with the above method is that it is not thread-safe. Consider the following execution sequence.

This execution sequence creates two objects for the singleton. Therefore this classic implementation is not thread-safe.

2. Thread-Safe (Synchronized)

Make getInstance() synchronized to implement Singleton Method Design Pattern

Example: Thread Synchronized Java implementation of singleton design pattern

  • Using synchronized ensures that only one thread at a time can execute getInstance().
  • This approach is safe for multithreading but can reduce performance due to the overhead of synchronization.
  • If performance is not critical, this method is a simple and reliable way to implement a singleton.

3. Eager Initialization (Static Block)

In this method, the Singleton instance is created at the time of class loading, regardless of whether it is used or not. It is simple and thread-safe, but may lead to unnecessary resource usage.

Example: Static initializer based Java implementation of singleton design pattern

  • The singleton instance is created in a static initializer, which runs when the class is loaded.
  • This approach is thread-safe because JVM handles static initialization.
  • Best used when the singleton class is lightweight and needed throughout the program execution.

4. Double-Checked Locking (Most Efficient)

Use “Double Checked Locking” to implement singleton design pattern

Example: Double Checked Locking based Java implementation of singleton design pattern

  • The obj is declared volatile to ensure correct handling by multiple threads during initialization.
  • This approach reduces the overhead of synchronizing every time getInstance() is called.
  • It provides a thread-safe and efficient way to create the singleton instance.

5. Static Inner Class (Best Java-Specific Way)

In Java, a Singleton can be implemented using a static inner class.

  • A class is loaded into memory only once by the JVM.
  • An inner class is loaded only when it is referenced.
  • Therefore, the Singleton instance is created lazily, only when the getInstance() method accesses the inner class.

Example: using class loading concept singleton design pattern

  • A private static inner class (SingletonInner) holds the singleton instance.
  • The instance is accessed via the getInstance() method of the outer class.
  • Being static, the inner class is loaded only once when INSTANCE is first accessed, ensuring lazy initialization and thread-safety.

6. Enum Singleton

In Java, a Singleton can also be implemented using an enum, which is the simplest and safest approach. Enums are loaded by the JVM only once, and each enum constant is created exactly one time. Therefore, the Singleton instance is created safely when the enum is first accessed.

Example: Enum-based Singleton

  • INSTANCE is the single allowed object of the enum.
  • The JVM ensures that the enum is thread-safe, created only once, and cannot be instantiated again.
  • It also automatically protects against serialization and reflection issues.
  • When Singleton.INSTANCE is accessed for the first time, the enum is loaded and the instance is initialized only once.

Implementation

Example: The implementation of the singleton Design pattern is very simple and consists of a single class.


Output
Singleton is Instantiated.
Something is Done.

Advantages

The Singleton pattern offers several benefits when controlled access to a single instance is required:

  • Ensures only one instance exists, preventing inconsistent states.
  • Saves memory and system resources by avoiding multiple object creation.
  • Provides a global access point, making it easy to share the same instance across the application.

Disadvantages

Despite its benefits, Singleton can introduce certain design challenges:

  • Can create tight coupling since many classes depend on a global instance.
  • Difficult to unit test due to shared global state.
  • May cause issues in multithreaded environments if not implemented carefully.
Comment
Article Tags:

Explore