Singleton design pattern is one of the simplest design pattern which ensures that only one object of a class can be created and it provides a global point of access to that instance.
It comes under creational design pattern as it provides one of the best ways to create an object.
This is useful when we need to have only one instance of our class for example a single DB connection shared by multiple objects as creating a separate DB connection for every object may be costly. Similarly, there can be a single configuration manager or error manager in an application that handles all problems instead of creating multiple managers.
class Singleton
{
private static Singleton instance;
// private constructor to restrict the instantiation of Singleton Class
private Singleton() {
}
//GetInstance method to provide global access to Singleton instance
public static Singleton getInstance()
{
if (instance==null)
instance = new Singleton();
return instance;
}
}
Refer the post for more details