Design Pattern #1 Singleton Pattern
Japneet Sachdeva
Become a successful SDET with my Road To Full Stack QA Courses, Guides & Newsletters
Design patterns are one of the most used solutions to improve a framework or code structure. Singleton pattern is part of creational patterns and deals with how an object is going to be created.
How Singleton pattern works?
By using this design pattern we focus on creating exactly one copy of class object. This one copy will now be shared with other classes if needs to be used again.
How can you incorporate Singleton Pattern?
Different ways to setup Singleton Pattern
1. Eager Initialisation
Using eager initialisation an instance of a class is created much before it is actually required. It is mostly done on system startup.
The singleton instance is created irrespective of whether any other class actually asked for its instance or not. This is done usually using a static variable as these get initialised at the application startup, always.
public class EagerSingleton {
private static volatile EagerSingleton instance = new EagerSingleton();
// private constructor
private EagerSingleton() {
}
public static EagerSingleton getInstance() {
return instance;
}
}
The above method works fine, but it has one drawback. The instance is created irrespective of it is required in runtime or not.
2. Lazy Initialisation
Lazy initialisation is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until it is needed the first time.
In the context of the singleton pattern, lazy initialisation restricts the creation of the instance until it is requested for the first time.
public class LazySingleton {
private static LazySingleton instance = null;
// private constructor
private LazySingleton() {
}
public synchronized static LazySingleton getInstance() {
if (instance == null)
{
instance = new LazySingleton();
}
return instance;
}
}
The above code works fine until we are using it with sequential execution, but the moment we move it into a Multi-Threaded Env. or Parallel execution. This setup fails and delays the execution.
Why does this delay occur?
As you can see in above code, we have used synchronized keyword in our method declaration, which means whenever a thread reaches this method 2 things will happen:
Which in turn delays the execution, hence to improve this performance we are going to use a synchronized block.
Updated Code:
public class LazySingleton {
private static volatile LazySingleton instance = null;
// private constructor
private LazySingleton() {
}
public static LazySingleton getInstance() {
if (instance == null) {
synchronized (LazySingleton.class) {
// Double check
if (instance == null) {
instance = new LazySingleton();
}
}
}
return instance;
}
}
Now with above code, if instance is null then only it enters the synchronized block and otherwise it will just return the instance without any delays.
I hope this makes easier for you to understand and implement singleton pattern into your frameworks. If you like such articles the consider subscribing to my newsletter link below. Also share it within your network if possible :)
Subscribe to newsletter: Link
SDET Manager Course: Link
Full Stack QA Course: Link
950+ SDET Interview Questions with Answers: Link
#japneetsachdeva
CEO/Principal: CERAC Inc. FL USA..... ?? ????????Consortium for Empowered Research, Analysis & Communication
4 个月Very helpful