In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton.
With Code in JAVA
2. Definition and types
Creational Patterns
Advantages and usage
Discuss Problem
Class diagram
Implementation
Lazy Instantiation
Easy Instantiation
Dealing with multithreading
3. Singleton Design Pattern comes under “Creational Design Patterns” category.
Creational design patterns are design patterns that deal with object
creation mechanisms, trying to create objects in a manner suitable to
the situation. The basic form of object creation could result in design
problems or added complexity to the design. Creational design
patterns solve this problem by somehow controlling this object
creation.
“ ”
4. In software engineering, the singleton pattern is a software design pattern that
provides the possibility to control the number of instances (mostly one) that are
allowed to be made. We also receive a global point of access to it (them)
In other words, a class must ensure that only single instance should be created and
single object can be used by all other classes.
There are two forms of singleton design pattern
1.Early Instantiation: Creation of instance at load time.
2.Lazy Instantiation: Creation of instance when required.
5. Imagine an intersection where several roads meet, and where the cars need to
ensure they do not crash into each other when crossing this intersection.
Assuming there were no traffic lights to guide them, we would need a police officer
standing in the middle to direct traffic. This officer would be a real life
approximation of a singleton.
The officer would receive visual queues from all roads, telling him which one is
filling up the most with cars. With all this information, he would be able to decide
which road to let the cars come through, and which roads to block.
This would be significantly more complex if there were several officers controlling
the intersection, where they would all be receiving visual queues from the roads,
would have to communicate with each other and then collectively decide which
road to let traffic come from.
6. Saves memory because object is not created at each request.
Only single instance is reused again and again.
In cases when object creation is very costly (time taking), we don’t have to create
new object each time we need it. We just access already created object.
When only one instance or a specific number of instances of a class are allowed.
It is used in logging, caching, web server, etc.
For database connection, because one connection is enough for most applications and
too much connections can make application slow.
7. If your program needs to do logging, you may want all output to be written to one
log file. That could lead to resource conflicts and errors.
objectA objectB objectC
loggerA loggerB loggerC
LogFile.txt
8. With a singleton logger object, you don’t need to worry about several different
objects trying to write to the same file, at the same time.
objectA objectB objectC
loggerSingleton
LogFile.txt
9. Singleton
- static uniqueInstance
// Other useful Singleton data...
- Singleton()
+ static getInstance()
// Other useful Singleton methods...
Note:: A class implementing the Singleton Pattern is more than a Singleton; it
is a general purpose class with its own set of data and methods
10. public class Singleton {
private static Singleton uniqueInstance;
// other useful instance variables here
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
11. public class Singleton {
private static Singleton uniqueInstance = new Singleton();
// other useful instance variables here
private Singleton() {}
public static Singleton getInstance() {
return uniqueInstance;
}
}
12. public static void main(String[] args) {
Singleton s = Singleton.getInstance();
Singleton r = Singleton.getInstance();
System.out.println (s);
System.out.println (r);
}
run:
15db9742
15db9742
BUILD SUCCESSFUL
13. Note that, the above code is this not thread safe. If you are working in multi
threaded system then you should not use above implementation.
For most Java applications, we obviously need to ensure that the Singleton works
in the presence of multiple threads.
Multithreading in java is a process of executing multiple threads simultaneously.
14. Our multithreading problems are almost fixed by making getInstance() a
synchronized method:
public class Singleton {
private static Singleton uniqueInstance;
// other useful instance variables here
private Singleton() {}
public static synchronized Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}