Home » Core Java » Singleton Design Pattern

Singleton Design Pattern

What is Singleton Design Pattern ?

Singleton design pattern comes under Creational design pattern type.From the name and type we can know basically this design pattern is  all about creating only a single instance of a class.But,this is not that simple how it sounds now,because it is not single instance,it is actually single instance per JVM. That means in one run time jvm ,one and only one instance of a particular class.So we can say “Singleton design pattern is a design pattern which helps us to create one and only one instance of a class per JVM run time

Example of Singleton design pattern ::Let’s see what are the examples of Singleton design pattern.Logger class of log4j is an example Singleton class.Your application will be using many threads internally to process codes and program logics and all threads of the application continuously write logs.When we have singleton then only your logs appear meaningful to you,else logs will be jumbled up by many threads.

How to create singleton design pattern :: You will find many articles and blogs who describe singleton design  pattern and how to create it.What I am going to do I will explain here only 2 ways to implement singleton which can impress the interviewer. 

Program 1::


In the above Program ,I have declared a class Singleton.
1. The above Singleton class has a private empty constructor which prevents us to create a instance of the class using new keyword.

(a) The above Singleton class has a static method getInstance().Since the getInstance() is a static method  it can be accessed using class name only,so we do not require to create instance of the class.

(b) getInstance() method is doing a null check o the instance and then a lock is defined at class level and inside the synchronized() block again we are doing null check of the instance.Suppose two  threads try to access the getInstance() method while the first thread acquires the lock and  is about the execute line no 5 “instance = new Singleton();” ,the second thread may be at line 2 and it found that instance is still null.After first thread create the instance ,the second thread acquires lock and again checks if instance is null or not and it found instance is not null so it return the already created instance.Now if we do not have line no 4″if (null == instance) { ” we may end up creating two instances even after using synchronized block.I have also made instance as volatile so that we never cache it and always read from main memory for  successfully handling the multi threading issues.If we do not use double  null check and only single null check then that is called lazy initialization and we should not do that.

3 . The above Singleton class also  implements Cloneable  interface and implement the clone() method inside which it is  calling the same getInstance().Since we are using volatile,it will return the already created same  instance.

4. The above Singleton class also  implements Serializable interface and implement the readResolve() method inside which it is  calling the same getInstance().During deserailization when ObjectInputStream has read an object from the stream using readObject() method and is about to return the object ,ObjectInputStream checks if the class of the object has  readResolve() method defined.Then readResolve method is called to allow the object in the stream to mention the object to be returned.In our case we have returned “Singleton.getInstance()” ,so it returns the same instance.

5. The above Singleton class single  instance creation can be broken by Reflection. Refection is very powerful and it can break all the above approaches.See the below code to create duplicateInstance.setAccessible(true) allows to access private methods and constructors and we can create duplicate instance.The reflection way of creating duplicate instance issue can be taken care by using enum which was introduced in jdk 1.5 version.

Program 2 ::


Leave a comment

Your email address will not be published. Required fields are marked *

four × one =