1
Software Design
Lecture : 30
2
Categories of Design Patterns
Creational
Structural
Behavioral
3
Singleton Design Pattern
Sometimes, there may be a need to have one and only
one instance of a given class during the lifetime of an
application.
This may be due to necessity or, more often, due to the
fact that only a single instance of the class is sufficient
For example maintaining single database connection,
registry values etc
4
Application of Singleton
In environments where resources are rare, you
may need to restrict how many counted instances
of a class are created
5
Possible Implementations
Using Global Variables
i. Having an instance of the class in a global variable seems
like an easy way to maintain the single instance.
ii. All client objects can access this instance in a consistent
manner through this global variable.
•.
6
Problem in the approach
This approach does not prevent clients from creating
other instances of the class
For this approach to be successful, all of the client objects
have to be responsible for controlling the number of
instances of the class
This is not desirable since client classes should be free
from class creation process
7
Solution
The responsibility for making sure that there is only one
instance of the class should belong to the class itself,
leaving client objects free from having to handle these
details.
8
Definition of Singleton Design Pattern
Singleton Design Pattern ensures that there is
only one instance of a class and provides
global point of access to it.
9
Class Diagram
10
11
Sample Java Code
12
public class Singleton
{
private static Singleton uniqueins;
private Singleton() // Private Constructor
{}
public static Singleton getInstance() // Class Method
{
if (uniqueins==null)
{
uniqueins= new Singleton();
}
return uniqueins;
}}
13
class testsingleton
{
public static void main(String args[])
{
Singleton.getInstance(); // call to static method
}
}
14
Singleton in Multithreaded Environment
There is a problem with the code above in a
multithreaded environment. Two threads might
get a hold of two different instances if they enter
at same time in the getInstance method.
15
Multithreaded Safe Code
16
public class Singleton {
private static Singleton uniqueinst;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}}
17
Problem with Solution
Synchronization decreases performance by factor of 100 !!!
This solution fixes our problem but it is very expensive.
Indeed we only need synchronization for the first call. After
that synchronization is totally unneeded.
18
Solution to Problem
DoubleChecked Locking
i. With this solution, we first check to see if an instance is created and
only then we synchronize.
ii. This way we only synchronize the first call and there's no
performance issue anymore.
19
Code with Double Checked Locking
20
public class Singleton {
private volatile static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized(Singleton.class) {
if (instance == null) {
instance = new Singleton();
}}}
return instance;
}}