Java Singleton Design Pattern

Java has several design patterns Singleton Pattern being the most commonly used. Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process. This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM
  • Intention :
  • 1) Ensure a class has only one instance, and provide a global point of access to it.
    2) Encapsulated "just-in-time initialization" or "initialization on first use".

  • Problem :
  • Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

  • Requirements to create a singleton object in Java :
  • 1) Provide a default Private constructor.
    2) Create a Method for getting the reference to the Singleton Object.
    3) Make the Access method Synchronized to prevent Thread Problems.
    4) Override the Object clone method to prevent cloning.

  • Step 1: Create a class Singleton

  • In given Program all the specified requirement for singleton pattern is satisfied i.e
  • Step 2: Made default constructor private

  • Step 3: Create a Method for getting the reference to the Singleton Object and make it as synchronised

  • Note:
  • 1) We write a public static getter or access method to get the instance of the Singleton Object at runtime. First time the object is created inside this method as it is null. Subsequent calls to this method returns the same object created as the object is globally declared (private) and the hence the same referenced object is returned.
    2) It could happen that the access method may be called twice from 2 different classes at the same time and hence more than one object being created. This could violate the design patter principle. In order to prevent the simultaneous invocation of the getter method by 2 threads or classes simultaneously we add the synchronized keyword to the method declaration.

  • Step 4: Override the Object clone method to prevent cloning.
  • 1) We can still be able to create a copy of the Object by cloning it using the Object’s clone method. This can be done as shown below

    SingletonObjectDemo clonedObject = (SingletonObjectDemo) obj.clone();
    2) This again violates the Singleton Design Pattern’s objective. So to deal with this we need to override the Object’s clone method which throws a CloneNotSupportedException exception.

  • Step 5: Create class SingletonObjectDemo to test the Singleton pattern

  • Run the given class as java Application and we will get the output As

  • Note:
  • To prove that only one object has been created for the Singleton class and if we try to make another one in SingletonObjectDemo class what will happen?

  • This proves that another object cannot be created for the Singleton Design pattern.
Sometimes in distributed systems, we need to implement Serializable interface in Singleton class so that we can store it’s state in file system and retrieve it at later point of time. Here is a small singleton class that implements Serializable interface also.
  • Step 1: Create a Singleton class with serialization

  • Step 2: Create a class to test that serialized singleton object persist or not.

  • Step 3: Run the Test Program to see output

  • This proves that singleton Object is not preserved while Deserialization, because both of the instances have different hashCode.
  • So it destroys the singleton pattern, to overcome this scenario all we need to do it provide the implementation of readResolve() method.

  • After this you will notice that hashCode of both the instances are same in test program.

  • This proves that the Singleton object has been serialized with both the instances having same hashCode.