Multiple ways of creating a Singleton

by Prasanth Gullapalli

Singleton pattern is one of the most widely used design patterns. When a class is called Singleton, there is only one instance created for it and it forms the global point of contact. This is implemented by making the constructor private and getInstance() method is given the responsibility of providing the access to the singleton object created. This can be pretty straight forward when we work in a single threaded environment. But when we have multiple threads, ensuring single instance creation can be little trickier. In this article I will discuss the multiple ways of creating a Singleton Class

1. Eager instantiation
2. Double Checked Locking Idiom
3. Initialization-on-demand holder idiom
4. Enum Singletons
Continue reading