Singleton
Problem
-
Ensure that a class has just a single instance
The most common reason for this is to control access to some shared resource—for example, a database or a file.
Here’s how it works: imagine that you created an object, but after a while decided to create a new one. Instead of receiving a fresh object, you’ll get the one you already created (not using constructor).
-
Provide a global access point to that instance
Maybe unsafe as any code can access the object and could potentially overwrite the content.
Singleton protects the instance from being overwritten by other code.
Solution
-
Make the default constructor private, to prevent other objects from using the new operator with the Singleton class.
-
Create a static creation method that acts as a constructor. Under the hood, this method calls the private constructor to create an object and saves it in a static field. All following calls to this method return the cached object.
If your code has access to the Singleton class, then it’s able to call the Singleton’s static method. So whenever that method is called, the same object is always returned.
Sample Code
Reference
How is this guide?