The Singleton Pattern is a design pattern used in software engineering to ensure that a class has only one instance, and it provides a global point of access to that instance. This pattern restricts the instantiation of a class to a single instance and ensures that no other instance can be created during the runtime of an application. It is commonly used for scenarios where you want to control access to resources or centralize control over some part of your system.
Here’s a typical implementation of the Singleton Pattern in a programming language like Java:
public class Singleton {
// Private static variable to hold the single instance of the class
private static Singleton instance;
// Private constructor to prevent instantiation from other classes
private Singleton() {
// Initialization code here
}
// Public method to provide access to the single instance
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
// Other methods and data members can be added here
}
In this example:
- The
Singleton
class has a private static variable calledinstance
to hold the single instance of the class. - The constructor of the
Singleton
class is made private, preventing other classes from creating instances of it. - The
getInstance
method is used to get the single instance of theSingleton
class. It first checks if an instance already exists and, if not, creates one. Subsequent calls togetInstance
return the same instance.
Using the Singleton Pattern ensures that there is only one instance of the class throughout the application’s lifetime, which can be beneficial for various scenarios, such as managing configuration settings, logging, database connections, and more. However, it should be used judiciously, as it can introduce global state and make code harder to test and maintain if overused.
But in the above example, if you run in the multithreaded environment sometimes it violates. So we should need to add the synchronize
keyword to the getInstance() method
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
After applying the synchronized keyword it works perfectly. But instead of applying the synchronized keyword to the whole method, we can use only the block of code.
public static Singleton getInstance() {
if (instance == null) {
synchronized(Singleton.class){
instance = new Singleton();
}
}
return instance;
}
But in multithreaded environment , it’s better to provide Double Lock bcz at the same time if another thready has the lock it will create the instance.
public static Singleton getInstance() {
1. if (instance == null) {
2. synchronized(Singleton.class){
3. if(instance == null){
4. instance = new Singleton();
}
}
}
7. return instance;
}
But sometimes double lock also will create the violation for Singleton Pattern. Suppose 2 threads are like t1, t2 are entered into the code it goes to the line no 1 to line 4 and create the instance. At the same time t2 enters and it knows that there is something in variable ‘instance’ (bcz java runtime publishes the half initialized variables). So it simply returns the line no 7 i.e. ‘return instance’ line.
To address the above issue, we can simply use the volatile keyword at the time of instance decleration. The volatile variable will be published only when the change completes.
private static volatile Singleton instance=null;
There are other approaches are also there i.e. Singleton Class Using an Inner Static Helper Class and another approach is By Using Enum create the Singleton class
Those 2 approaches we will discuss in the next post. Thank you