This pattern allows you to create a single object for your class within your entire application.
For example, you use the Calendar
class in multiple places of your application, accessing your instance through the getInstance()
method. Everywhere you are using, the instance of Calendar
is the same, you do not give new Calendar()
, you use getInstance()
which is the method that manages the uniqueness of the object.
In this way the Singleton pattern guarantees that a class has only one instance in the entire application, managing it from within the class to prevent another class from creating another instance.
The advantage is that the Singleton pattern can be instantiated and used only when needed, differently if we create a global variable in which the object is always created when the application is initialized and may be using features that are not needed at this time. The Singleton standard defines a single point of global access and is much easier to manage the creation and use of the instance.
More information and implementation.