Tuesday, 9 February 2016

Create Singleton Class in java

Create Singleton Class

Creating Singleton Class in Java- Tech blog
In this article, I will show how to write Singleton class in JAVA

Definition:- A class called Singleton class if we can create one and only one instance of that class.

Fact about Singleton class

  • Singleton class is mainly used to control the object creation.
  • If required, we can allow more object. But then it won’t be called as as Singleton.
  • Since there is only one object of Singleton class, any instance variable will be occurring only once just like static fields.
  • Singleton will be used to maintain the access of resource creation. Ex. database connection, network sockets etc.
  • Servlet in Java and HibernateSessionFactory are real example where Singleton is required.Servlet is by default implements singleton, while HibernateSessionFactory are configured to serve as Singleton.
Now let us do some practical.

To make any class Singleton, we need to make sure about following 3 things



  • Constructor of that class must be private
  • Create one private static variable of type that class only.
  • Create one public static method which will actually create instance of that class and assign to this static variable.

There are two flavor of Singleton class as below
  • Instantly
  • Lazy
both flavor will follow all 3 things which are must for Singleton class. The only difference between in them is the time when they will create the instance of class. 
Instantly flavor will create the instance of class as soon as it loaded in the memory while Lazy flavor will create instance when very first request will come.
1. Instantly flavor of Singleton class

Below snippet represent Instantly flavor


You can notice that …


  • first we have created static instance variable of type same class. 
  • Second we created private constructor and private static method that will actually create the instance as soon as this class loads into memory. While creating the instance variable we are assigning its value through this private static method.
  • That is the reason, its called Instantly flavor Singleton class.
  • This flavor had tow static methods. One is private which actually create the instance while another is public which actually return the instance.

2. Lazy flavor of Singleton class

Below snippet represent the Lazy flavor of Singleton class




You can notice that…


  • First we created private static instance variable of type same class.
  • But this time we are not creating the instance so setting it to null
  • Next we created private constructor so nobody will create instance of this class
  • At the last we created public static method which will check if the instance variable is null then it will first create it and then return it. 
  • This way, the public static method will only create the instance when we first time call it. Second and sub-sequence call will get same object.
  • That is why we are calling it Lazy because the instance will be created on first call.
You may put some logger/SOPs in between to know how its works

No comments:

Post a Comment