Singleton Pattern
Byadmin on Mar 27, 2008 | In Gang Of Four Patterns
Intent
Ensure a class only has one instance, and provide a global point of access to it. - GoF
Type
Object Creational
Solution
The Singleton Pattern is probably the best-known pattern of all the “Gang of Four” patterns. It is also one of the simplest patterns, but if not used correctly, the one with the most pitfalls and problems.
The Singleton Pattern should be used when there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point. The pattern is also used when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.
The Singleton Pattern can be implemented in Java in one of several ways. The most important though across the different implementations is to hide the operation that creates the instance of the class. This implies that the constructor of the class should be made private, so that it is not possible for other classes to access the constructor of the Singleton class. The second is to create a single point of access to the instance through a class operation that has access to the variable that holds the unique instance.
The creation of the single instance could occur in more than one place. The first is to initialize the class the moment an instance of the class is requested. This means that the class is initialized the moment the single point of access to the singleton object is invoked. A validation should occur to ensure that an instance is not already created (See Example 1).
The second is to initialize the instance when the class is loaded during the start-up and loading of the classes by the Class Loader. This is achieved by statically initialise the variable that holds a reference to the Singleton object (See Example 2).
Structure

Consequences
Listed below are some of the trade-offs and benefits of using the Singleton Pattern:
- Provides controlled access to sole instance. The Singleton Pattern provides a single access point to the instance of the class through the getInstance method. The constructor is private which makes the creation of the class only possible from within the class itself.
- Ability to manage the number of instances. The Singleton Pattern enables you to manage the number of instances. By making small adjustments to the code, a set number of instances can be managed by the pattern.
- Single Instance not guaranteed in enterprise environments. The single instance is only guaranteed in the scope of the class loader of a specific JVM. This means that in a Enterprise Cluster, where the application is run across multiple JVM's, the single instance is not guaranteed.
Java Sample Code
Example 1:
The first example "SingletonPattern1.java" explains how to create the Singleton Pattern with the use of the synchronized keyword. The code in line 24 shows the static variable that holds a reference to the Singleton instance.
Code:
private static SingletonPattern1 SINGLETON_INSTANCE = null; |
The code in line 42 shows the single point of access to the Singleton instance. The method first check if the instance exists, and instantiate the class if the reference to the instance does not exist. The instance is assigned to the variable and returned to the requesting object.
Code:
public static synchronized SingletonPattern1 getInstance() { | |
if (SINGLETON_INSTANCE == null) { | |
SINGLETON_INSTANCE = new SingletonPattern1(); | |
} // if | |
| |
return SINGLETON_INSTANCE; | |
} // method getInstance |
Download Example 1
Example 2:
The second example "SingletonPattern2.java" explains how to create the Singleton Pattern by statically initializing the class. The code in line 24 shows the static variable that holds a reference to the Singleton instance.
Code:
private static SingletonPattern2 SINGLETON_INSTANCE = new SingletonPattern2(); |
The class is initialized during the loading of the class during application start-up. The single point of access method therefore only need to return the instance.
Code:
public static SingletonPattern2 getInstance() { | |
return SINGLETON_INSTANCE; | |
} // method getInstance |
Download Example 2
References
- Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995
| « Design Pattern - Prototype Pattern | Distributed Multitiered Architecture » |






