Portfolio

Blog

My articles

Singleton Design Pattern

The Singleton pattern ensures that a class has only one instance and provides a global access point to that instance. In this article, we will examine the Singleton design pattern, its use cases and present a sample program illustrating its implementation.

What is the Singleton Design Pattern?

  1. The Singleton pattern is one of the creational design patterns, aiming to restrict the instantiation of a class to a single instance and provide a global point of access to that instance. This ensures that there is only one instance of the class throughout the application’s lifetime. The Singleton pattern is useful in scenarios where only a single instance of a class is required to control actions, coordinate tasks, or serve as a single point of management.

    Key characteristics of the Singleton pattern include:

    1. Private constructor: The class has a private constructor, preventing external instantiation.
    2. Static instance: The class provides a static method to access the single instance of itself.
    3. Lazy initialization (optional): The instance is created only when it is first requested, not when the class is loaded.

Use Cases for Singleton Pattern

  • The Singleton pattern is applicable in various situations, including:

    1. Database Connections: When you need a single point to manage database connections to avoid resource wastage.

    2. Logging: To maintain a single log file throughout the application’s execution.

    3. Caching: When caching expensive resources, such as database queries or network requests, to improve performance.

    4. Thread Pools: Ensuring that only one thread pool instance exists to manage concurrency.

    5. Configuration Management: Managing application-wide configurations.

    6. Driver Objects: For hardware access or managing resources like a printer.

Implementing the Singleton Pattern

Let’s create a simple  program to implement the Singleton pattern.

public class Singleton {
private static Singleton instance;

private Singleton() {}

public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void showMessage() {
System.out.println("Hi, I am a Singleton!");
}
}

In this example, we have:

  • A private static instance variable instance to hold the single instance of the class.
  • A private constructor to prevent external instantiation.
  • A public static method getInstance() that returns the single instance of the class. It uses lazy initialization, creating the instance only when it’s first requested.
  • A showMessage() method to demonstrate that you can add your own methods and attributes to the Singleton class.

Now, let’s use this Singleton class in our main program:


public class Main {
public static void main(String[] args) {

Singleton singleton = Singleton.getInstance();

singleton.showMessage();
}
}

In this main program, we obtain the Singleton instance using Singleton.getInstance(), and then we call the showMessage() method to prove that we are indeed working with the same instance throughout the program.

The result of code execution is as follows:

Hi, I am a Singleton!

Conclusion

The Singleton design pattern ensures that a class has only one instance and provides a global point of access to that instance. It is a useful tool for managing resources, controlling access, and coordinating actions in your Java applications. When implementing the Singleton pattern, remember to handle synchronization if your application is multithreaded and consider using the Bill Pugh Singleton or Enum Singleton to ensure thread-safety and avoid potential issues.

Web Developer

© Michał Pieróg. All Rights Reserved.

Scroll to Top