Singleton Design Pattern…
According to the Gang of Four, the singleton design pattern is categorized under creational pattern and that provides the best ways of object creation. This pattern is mostly used design pattern in Java. When it comes to object creation of this pattern, it only allows one instance per container. In the case of Java, it creates a single instance of a class per JVM and it provides a global point of access to that instance.
Now you might think about why we need only one instance of a class or why we need to restrict the limit of object creations to only one, this frequently guarantees that there is access control to resources, for example, database connection or socket. Most of the time, some programmers use this singleton design pattern without having a good understanding of where or when to apply this pattern. Therefore, make sure not to overuse this pattern as it is kind of really hard to do unit testing due to it being tightly coupled with a singleton instance.
As a programmer, when we implement a singleton pattern,
- we must make the constructor of the singleton class ‘private’ in order to restrict the instantiation of the class from other classes,
- we must create a static variable for instance and that should not take any arguments.
- we must implement a static method to provide a global access point for the singleton object.
Apart from that, we can use the approaches such as the ‘Lazy Initialization Method’(Instances are created only when required.), ‘Early Initialization Method’ (Instances are created at load time), and ‘Double-Checking Singleton’ to make thread-safe singleton using the ‘Synchronized’ keyword when we implement the singleton pattern.
Now assume, there is a small counseling office where each counseling session is taken through an online video call and only one session handler is there to handle every session. However, this person can only have one calling session at a time which means that 2/ more clients cannot have to call a session with the session handler at the same time.
Now let’s try to come up with the implementation of the singleton pattern which guarantees that only a single session handler can be created.
CounselingManager class —
CounselingManagerApplication class —
According to the above output, we are clear that though we have created two instances from CounselingManager class, it gave us the same instance. The implementation of the CounselingManager class included getCounselingManager() method and that checks for an instance of CounselingManager. When implementing CounselingManager class, to ensure thread safety of the implementation, it used the ‘double-checking singleton’ approach which helps to prevent edge cases such as two or more threads may creating an instance.
Most of the developers use this ‘Synchronized’ keyword instead of ‘static’ in a method-level declaration which leads to getting a performance hit due to blocking everything. Therefore remember to use the ‘Synchronized’ keyword in the correct place in order to block the exact code segment that you want to block rather than blocking everything.
Overuse of singleton leads to giving some problems to your implementation and difficulty of unit testing can be marked as drawbacks of Singleton whereas saving memory due to creation of a single instance and easy maintenance due single point of access to a particular instance can be marked as advantages of Singleton pattern.
So here you come to the end of the Singleton pattern discussion. and I hope you all are clear somewhat of Singleton.