The singleton pattern is a design pattern that restricts the instantiation of a class to one object. Using following techniques we can make instantiation singleton.
1)Lazy initialization:
// Classical Java implementation of singleton
// design pattern
class
Singleton
{
private
static
Singleton obj;
// private constructor to force use of
// getInstance() to create Singleton object
private
Singleton() {}
public
static
Singleton getInstance()
{
if
(obj==
null
)
obj =
new
Singleton();
return
obj;
}
}
Singleton obj is not created until we need it and call getInstance() method. This is called lazy instantiation. Singleton obj is not created until we need it and call getInstance() method. This is called lazy instantiation.
The main problem with above method is that it is not thread safe.
Using synchronized makes sure that only one thread at a time can execute getInstance().
The main disadvantage of using synchronized every time while creating the singleton object is expensive and may decrease the performance. However if performance of getInstance() is not critical for your application this method provides a clean and simple solution.
2)Eager initializartion:
// Static initializer based Java implementation of
// singleton design pattern
class
Singleton
{
private
static
Singleton obj =
new
Singleton();
private
Singleton() {}
public
static
Singleton getInstance()
{
return
obj;
}
}
Here we have created instance of singleton in static initializer. JVM executes static initializer when the class is loaded and hence this is guaranteed to be thread safe.
3)Double-checked locking:
If you notice carefully once an object is created synchronization is no longer useful because now obj will not be null and any sequence of operations will lead to consistent results.
So we will only acquire lock on the getInstance() once, when the obj is null. This way we only synchronize the first way through, just what we want.
class
Singleton
{
private
static
volatile
Singleton obj =
null
;
private
Singleton() {}
public
static
Singleton getInstance()
{
if
(obj ==
null
)
{
// To make thread safe
synchronized
(Singleton.
class
)
{
// check again as multiple threads
// can reach above step
if
(obj==
null
)
obj =
new
Singleton();
}
}
return
obj;
}
}
This method drastically reduces the overhead of calling the synchronized method every time.