In many cases, it is important for some classes having at most one Instance. There might exist many printers in the system, but there should be only one printer spooler.
But how can we ensure, that there is only one Instance and that this instance is easily accessible? A global variable would make the Instance accessible, but it can't keep you from instantiating more than on object.
A better solution is to make the class itself responsible for watching over its status and its sole instance. The class can ensure that only one instance is created by hiding the constructors, and it can provide a way to access the instance. This is called the Singleton pattern, it consists of just one class, all methods which provide a way to access the instance are usually static.
You should use the Singleton pattern when
Facade, Abstract Factory, and Object Pool usually use the singleton pattern to assure only one instance of their classes.
If containers like Spring are used for component creation, it is typically not required any more to implement the Singleton pattern manually. The same is true for many applications of the factory pattern. In Spring the configuration of a component (Java bean) can be defined in the Spring-config file. On option is to retrieve the component as Singleton. Spring then deals with the lifecycle of the object.
public class PrintSpooler {
// Private constructor suppresses generation of a (public) default constructor
private PrintSpooler() {}
private static class SingletonHolder {
private static PrintSpooler instance = new PrintSpooler();
}
public static PrintSpooler getInstance() {
return SingletonHolder.instance;
}
}