Singleton design pattern is the most commonly used and easiest design pattern but we don’t have much awareness of this pattern also. I hope this article will help you.
Singleton means one instance and a singleton class is a class that can have one and only one instance at a time. It means that, at any one time, there will be one instance for that class. And, The single object can have a global access point. It means that anybody can use the object in a particular application.
It is often used for managing pools of resources like database connection and thread pool (Multi Threading).
We should be clear in using a pure singleton design pattern.
- when you create an instance, there should not be any arguments.
- we can not use singleton wherever it is possible. we should think and decide the possibilities.
Implementation of singleton design pattern.
There are two forms of singleton design pattern
- Early initialization : creating an instance at load time
- lazy initialization : creating an instance when needed
How to make a class a singleton class
To make a singleton design pattern, the particular class must be built with the following instructions.
The class must have :
- a private static instance: This is the single instance and gets memory once as it is declared static.
- a private constructor: It prevents the instantiate the singleton class from creating any other instance of the class from outside.
- a public static factory method: This one provides global access to a single object. As it is declared static, It can be called with a class name without creating a new instance.
let’s see the below example for Early initialization. In Early initialization, The instance is created at loading time.
In this case, like the singleton rule, there should be a single object does not matter how many times the instance is created,
let’s see the output of the above code:
Hence, there is the same hashcode for the two created objects of the singleton class DBManager.It means that they are the same object. So, we can say that there are the same objects.
Ok next, Let's see the problems in Early initialization.
- Not thread-safe in multiple threads access(concurrency access)
- There is a chance to create multiple same objects, It does not make sense. So the solution is that design the lazy initialization.
Lazy Initialization, creating the instance of the class when we need the instance only at the same time there is perfect one instance at a time too.
To make lazy initialization, we have to have the factory method in the synchronized method or create a synchronized block to get an instance initialization.
But, Before going to the synchronized method or block, let’s see what happen without the synchronized.
let’s see the below code snippet
This also creates two same objects. Yes, one thread comes and check, then creates an instance, meanwhile, another thread comes and checks for creating an object. Both threads get true for the object is null. so they create separate objects as well.
So. It is also an issue with the single instance concept of a singleton design pattern.
Therefore, We have to use the synchronized. Let’s see the below code.
The above code snippet provides the pure singleton design pattern actually. That is, One thread comes and checks then creates an instance, second thread comes and checks, if there is one instance already created, then it waits until the other object comes to null as we use the synchronized block here.
This is named as double-checking singleton.It is best practice when use th esignleton design pattern.
And, here we use the synchronized block, not a synchronized method.Because.If we use the synchronized method, then it will block all the statements in the factory method.
problems in singleton design pattern
- Unit testing is more difficult because it provides global access to the application
- we need a clear understanding of where we can use the singleton pattern
- lock the paralysis process in a multi-threaded system.
Let’s see about another design pattern in the next article.