Thilan Dissanayaka Software Architecture Apr 26

Singleton Pattern explained simply

Ever needed just one instance of a class in your application? Maybe a logger, a database connection, or a configuration manager? This is where the Singleton Pattern comes in — one of the simplest but most powerful design patterns in software engineering.

This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

What is the Singleton Pattern?

At its core, the Singleton Pattern ensures that:

  • Only one instance of a class is ever created.
  • It provides a global access point to that instance.
  • You control the lifecycle of the object — no more unnecessary setups.

A Quick Example (Without Singleton)

Take this simple Logger class:

public class Logger {
    public Logger() {
        // Perform setup operations
        System.out.println("Logger setup operations performed.");
    }

    public void log(String message) {
        // Log the provided message
        System.out.println("Logging: " + message);
    }
}

public class MainProgram {
    public static Logger MyGlobalLogger = new Logger();

    public static void main(String[] args) {
        System.out.println("Main program started.");

        // Some code here...

        // We might not need to log anything, but the logger setup still occurs.

        // If we need to log something:
        MyGlobalLogger.log("This is a log message.");
    }
}

Here’s the problem: Even if we never log a message, the logger still gets created — wasting resources if the setup is heavy.

In the above code, the Logger object is created even if we never call the log() method. This is inefficient if the setup operations are resource-intensive.

⚡ Not ideal!

Enter Singleton Pattern

With Singleton, we create the object only when needed, and only once.

Here’s how:

public class Logger {
    private static Logger instance;

    // Private constructor to prevent instantiation from outside
    private Logger() {
        // Perform setup operations
        System.out.println("Logger setup operations performed.");
    }

    // Static method to get the singleton instance
    public static Logger getInstance() {
        if (instance == null) {
            instance = new Logger();
        }
        return instance;
    }

    public void log(String message) {
        // Log the provided message
        System.out.println("Logging: " + message);
    }
}

Next we can use this in our main program. Unlike in previous code we don't create an object. Access the logger through the getInstance() method

public static Logger MyGlobalLogger = Logger.getInstance();

MyGlobalLogger.log("This is a log message.");

What if we try to create an object like this?

public static Logger MyGlobalLogger = new Logger();

This will cause a compilation error because the constructor is private and cannot be accessed from outside the Logger class.

Why use lazy instantiation?

  • Objects are only created when it is needed
  • Helps control that we’ve created the Singleton just once.
  • If it is resource intensive to set up, we want to do it once.

Handling Multi-Threaded Access

What would happen if two different threads accessed this line at the same time?

public static Singleton getInstance()
{
    if (instance == null) {
        instance = new Logger();
    }
}

In a multi-threaded environment, two threads could simultaneously pass the if (instance == null) check and create multiple instances. This breaks the Singleton pattern.

Synchronized Method (Thread-Safe but Slow)

public class Logger {
    private static Logger instance;

    private Logger() {
        System.out.println("Logger setup operations performed.");
    }

    public static Logger getInstance() {
        synchronized (Logger.class) {
            if (instance == null) {
                instance = new Logger();
            }
        }
        return instance;
    }

    public void log(String message) {
        System.out.println("Logging: " + message);
    }
}

This ensures thread safety but synchronizing the entire method may degrade performance.

Double-Checked Locking (Thread-Safe and Efficient)

public class Logger {
    private static volatile Logger instance;

    private Logger() {
        System.out.println("Logger setup operations performed.");
    }

    public static Logger getInstance() {
        if (instance == null) {
            synchronized (Logger.class) {
                if (instance == null) {
                    instance = new Logger();
                }
            }
        }
        return instance;
    }

    public void log(String message) {
        System.out.println("Logging: " + message);
    }
}
  • Volatile Keyword: Ensures visibility of changes to variables across threads.
  • Double Check: Reduces the need to synchronize after the object is created.

Local Variable Optimization: Reduces the need to access the volatile variable multiple times, improving performance.

public class Logger {
    private static volatile Logger instance;

    private Logger() {
        System.out.println("Logger setup operations performed.");
    }

    public static Logger getInstance() {
        Logger result = instance;
        if (result == null) {
            synchronized (Logger.class) {
                result = instance;
                if (result == null) {
                    result = new Logger();
                }
            }
        }
        return result;
    }

    public void log(String message) {
        System.out.println("Logging: " + message);
    }
}

Use Cases for Singleton Pattern

  • Logging: Ensure consistent logging across the application using a single logger instance.

  • Database Connection: Manage a single connection pool object for handling database interactions.

  • Caching: Store frequently accessed data in memory for quick retrieval.

  • Configuration Management: Centralize application-wide configuration settings.

  • Thread Pools: Maintain a single thread pool instance to control and reuse worker threads.

  • Device Drivers: Ensure a single access point for hardware resources like printers or sensors.

  • Authentication Manager: Manage user sessions and authentication checks consistently.

  • Service Locator: Provide access to a globally available service without repeated instantiation.

The Singleton pattern is useful when you need a single, shared object to manage global state or resource-intensive operations. Among the different approaches, Bill Pugh Singleton Design is the most efficient and widely recommended approach for modern Java applications.

ALSO READ
Build A Simple Web shell
Mar 23 Application Security

A web shell is a type of code that hackers use to gain control over a web server. It is particularly useful for post-exploitation attacks, and there are various types of web shells available. Some of....

GraphQL - Interview preparation guide
Oct 01 Interview Guides

## What is GraphQL? GraphQL is a query language for APIs and a runtime for executing those queries. It allows clients to request exactly the data they need, reducing over-fetching and....

How does SLL work?
May 17 Cryptography

Every time you see that small padlock icon in your browser's address bar, you're witnessing one of the internet's most important security technologies at work. This tiny symbol represents....

Finding the Second Largest Element in an Array
Nov 10 DSA

Hi all, Here I'm back with another algorithmic problem. This is a classical interview question asked almost everywhere. As you may know, I recently did a Software Engineering internship at WSO2.....

Understanding Assembly Language: Purpose and Structure
Mar 23 Low level Development

Assembly language is a low-level programming language that provides a human-readable representation of a computer's binary instructions. Unlike high-level languages like C, C++, or Python, which are....

REST API - Interview preparation guide
May 08 Interview Guides

## What is a REST API? A REST (Representational State Transfer) API is an architectural style for designing networked applications. It uses standard HTTP methods to interact with resources, making....