Singleton Pattern Made Easy


In the above image, there is only one glowing bulb, and if someone wants some light, they will always use the same bulb, which is glowing.


But why does everyone chooses the same bulb?


It’s because it is the fastest way to get a glowing bulb. After all, for other bulbs, we need to illuminate it first. Hence, choosing a glowing bulb is the most efficient way.

There is one more important observation inferred from the above image: when I am saying that everyone will choose the same glowing bulb, I am considering that they only have options shown in the above image. It means that a glowing bulb is one of its kind in the given scope.


The above explanation might have given the general Idea of Singleton.


By definition, Singleton means one of its kind in a given scope.

Let’s try to see some more examples to develop a better understanding of Singleton, before seeing any actual code.


Some more examples

  • You won’t buy a new TV every time you want to watch Netflix. You will use the one which you already have in your living room.

  • You won’t buy or rent a new home every evening when you come back from the office. You will go to the one which you already own or rent.

  • You won’t buy a new car every time you want to go somewhere.


Technical Implementation

Before knowing how to implement it, we should know when and why.


When?

If your scenario demands a single instance of some object, then you can opt for a singleton pattern.

  • Logging

  • Database

  • Caching


Why?

  • It provides efficient use of memory because there is only an instance of an object in JVM (Java Virtual Machine).

  • It provides fast access to objects because you won’t construct a new object every time you need it.


How?

  1. Create a private constructorThis avoids the instantiation of a class.

  2. Create a private static variable of the same classThis ensures that only one instance is available.

  3. Create a global access point To allow access to the private static variable mentioned in the above point.

Example Code


Cons

  • Unit Testing is hardSingleton class, tightly couples with client class, so we inescapably test singleton classes as well along with client class.

  • Requires additional implementation in Multi-Threaded EnvironmentDifferent threads can have different instances of same Singleton class that will result in inconsistency. Some extra efforts can avoid this con. (by using synchronized keyword, or by Bill Pugh Singleton implementation)



  • Tight CouplingBecause of incapability to create new instances, we can’t inject it in client class.

  • Reflection AttackPrivate constructor ensures a single instance but using a reflection technique, access modifier can be updated from private to public. This con can be avoided with some extra efforts. The solution to this will be discussed in future blogs.


I hope you enjoyed reading and learned about the Singleton Pattern. Stay tuned for more design patter.


Comments and feedback are most welcomed.


Please follow me on Linkedin, Github, and join our newsletter to keep yourself updated.


Thanks for reading. Happy Learning 😊

22 views

Recent Posts

See All