This pattern allows you to create a unique object for your class within your entire application.
For example, you use the class Calendar
in various places of your application, accessing your instance through the method getInstance()
of the same. Everywhere you are using, the instance of the Calendar
It’s the same, you don’t give a new Calendar()
, you use the getInstance()
which is the method that makes managing the oneness of the object.
This way the Singleton standard ensures that a class has only one instance in the entire application, managing it from within the class in order to prevent another class from creating another instance.
The advantage is that the Singleton standard can be instantiated and used only when necessary, unlike if we create a global variable where the object is always created when the application is initialized and may be using resources that are not currently needed. The Singleton standard defines a single global access point and is even much easier to manage the creation and use of the instance.
More information and implementation.