Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
Singleton pattern is useful when it makes sense to only have on instance of a class. For example, when working with a database or even a text file, oftentimes it is advised to only work with one single connection instance to avoid incosistencies and issues that could arise when multiple instances access the same resource. Aside from this, Singleton pattern is resource friendly, meaning that by creating only one instance, we do not waste memory for a new object when we actually do not need a new one. It also provides more flexibility in the sense that the class itself controls the instantiation process, so the class instantiation process can be adjusted/modified in one place.
The most common way to implement the pattern follows:
The code sample above provides the following pattern:
- Make the default constructor private, to prevent other objects from using the
newoperator with the Singleton class.
- Create a
staticcreation method that acts as a constructor.
Singleton design pattern is used in core java classes, for example
Quote Form JDK Documents
Every Java application has a single instance of class
Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the
An application cannot create its own instance of this class.
Runtime.getRuntime(); is similar to the
When to use this pattern?
Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program; a single logging object, hardware drivers' objects, etc.
You can be sure that a class has only a single instance. Moreover, you gain a global access point to that instance.1
- Unlike global variables that are unsafe (since any code can potentially overwrite the contents of those variables and crash the app), the Singleton pattern lets you safely access some object from anywhere in the program; it protects the single instance from being overwritten by other code.↩