Singleton Pattern
Classification: Creational Pattern Description : Ensure a class has only one instance, and provide a global point of access to it.
Whenever I hear about Singleton ‘Forever Alone’ troll face comes to my mind ;)
In Singleton pattern, there will be only one object and wherever we want to use this class, this only object will be used. While creating this, we have to ensure that no more than one object is created for this class.
There are 3 ways of creating it : 1. Traditional way without thread safety, where constructor is made private and a public method returns reference to the only object created. 2. Thread-safe way : Double check locking 3. Thread-safe way : Using enums (this is for Java of course!)
Read about Singleton Pattern here. The enums’ way of doing things is explained here.
The sample program in Git shows the first 2 ways. Find it here on Github