What Is the Singleton Design Pattern?

The singleton pattern is one of the simpler design patterns. A class that uses the singleton design pattern has a single instance that it manages on its own. This class prevents any other class from creating an instance of it.


A singleton class also provides a single global access point to the instance that it creates. So, any class that wants an instance of a singleton class, needs to access it through its single access point.

Java’s excellent support for object-oriented programming makes it easy to use the singleton design pattern.


Implementing the Singleton Pattern Using Java

There are many ways to implement the singleton pattern in Java; the eager and lazy approaches are common variations. Each of these approaches has its own merits and drawbacks. Therefore, the method you choose to employ should depend on how your application will operate.

The Eager Approach

Implementing the singleton pattern with the eager approach means that the class creates a new instance of itself when it loads.

public class EagerSingleton {
private static EagerSingleton instance = new EagerSingleton();

private EagerSingleton() {}

public static EagerSingleton getInstance() {
return instance;
}
}

The EagerSingleton Java class creates a new instance of itself as it loads. It assigns this instance to the private static instance variable, which is only accessible within the singleton class. The only point of external access to the instance variable is through the getInstance() method. That method returns the previously created instance of the class.

This approach is great as it prevents the multithreading problem, which is one of the biggest challenges of the singleton pattern. It solves the multithreading problem by creating a new instance of itself before any new thread can access its instance variable. This guarantees that each thread will only have access to the same instance.

However, the eager approach is only practical if your application will use an instance of the singleton class as it starts running. Otherwise, you’ll create an object before your application needs it, using resources unnecessarily.

The Lazy Approach

The lazy approach is the solution to the eager approach problem. It allows you to create a new instance of a singleton class only when your program needs it.

public class LazySingleton {
private volatile static LazySingleton instance;

private LazySingleton() {}

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

return instance;
}
}

The lazy approach solves the multithreading problem using the synchronized keyword. This prevents two threads from gaining access to the instance variable at the same time. Synchronized is expensive, though, so the application only uses it once, when it first calls getInstance().

When to Use the Singleton Pattern

The Singleton pattern is useful in many scenarios, to create dialog boxes, handle registry settings, or manage thread pools.

Another common use for the singleton pattern is to create a central class that manages database connections.

We use cookies to give you the best experience.