Singleton Design pattern Tutorial

 

Singleton design pattern plays efficient role in java during single object creation per context. This will come under creational patterns. Singleton pattern is a design pattern that restricts the instantiation/object creation of a class to only one object

There are few ways we can implement this pattern in java, but there are few disadvantages as well in the way we selected.
“Bill pugh” method is the best way to implement Singleton design pattern in java, this is implemented based on “Initialization-on-demand holder idiom”.

This pattern is best because, “OnDemandHolder is loaded for the first call of Singleton.getInstance() or for the first access to OnDemandHolder.INSTANCE, not before”

Below is the sample code.

Posted in designpatterns and tagged , .