Singleton Pattern

The Singleton pattern ensures class has only one instance and provides a global access to it. In other words, it says that singleton pattern makes it impossible to instantiate the class except for the first time and whenever you want an instance you must use the same instance.

There is a good argument on why singletons should not be used, and it would become a code smell in the future because singletons can only be used when we are 100% sure that now and in future, we only need only one instance of the class, but we would never know that in beforehand.

There is a saying that one man’s constant is another man’s variable which a profound statement in case of Singleton as it makes unit testing very difficult. Every time you request for an instance you get back the same instance of that class which makes mocking it very difficult because you have this global single instance which you are always reaching for. So even in the application if you need a single instance but in testing there is always a requirement for second instance of it.

Example

One of the examples where singleton is widely used is in using the same logger instance to write to the file and the reason, we do that If you are writing on a file system, having more than one instance and therefore, probably, more than one thread may result in a garbled file. In the sense that depending on buffering and other low-level mechanisms messages from one write may end up mixed with messages or parts of messages from others.

If you wouldn’t use a singleton class you would have to deal with the synchronization writing to a file, or whatever stream you use between these different logger instances. So, it’s much easier, when you just have one global Logger instance. Also, as I was saying previously mocking logs which are singleton are very painful and I have personally experienced these issues.

UML

Consider an example where you only need one instance of a class and which has global access. Let’s call this class Singleton.  This can be achieved by making the constructor private for that class which prevents the construction of that class from outside. So essentially you are preventing all clients from doing new Singleton.

But now the question is how we can create the first instance when we make the constructor private. That’s when we use static methods. Since static methods are accessible by class name and this static method has access to private constructor which can do the creation of the instance. In the above UML diagram this static method is called GetInstance(). Then we also have a private static variable of type Singleton called instance which is not accessible to client. GetInstance method would check if this static variable instance is null. If its null it creates an instance, saves it and sends that instance else it would just return the existing instance.

Code

Below is an example of creating a singleton class. The key thing to note here is that the constructor is private. We use static method to return an instance of the class by checking if we don’t have any instance previously. We also double check to lock the Singleton instance to avoid issues when we run it on multiple threads.

GitHub: Singleton Pattern

#16DayOf100DaysOfCode

Last modified: March 27, 2019

Author

Comments

Write a Reply or Comment

Your email address will not be published.