Tuesday, April 23, 2019

Singleton Class In Java

Q- What is singleton in java ?

Answer:- There should be only one instance allowed for the class in all the situation and we should provide global point of access to that instance.

Number of ways  to create singleton class :- 
  • Eager initialization or initialize before use
  • Lazy initialization or initialize as and when we need
  • Singleton using Inner class
  • Singleton with serialization and de serialization
  • Using Enum
  • A thread-safe singleton pattern in java using Synchronization
  • Double-checked locking
  • Double-checked locking with volatile keyword
  • Thread-safe but not lay initialized
  • The ultimate Thread-safe and efficient singleton pattern in Java 
Q- How to write singleton class in java ?
Answer :

Singleton.java
package design.patern.singleton;

public class Singleton {

 private static Singleton instance = null;
 
 // Constructor should be private
 private Singleton() {
 }

 // Create instance of class
 public static Singleton getInstance() {
  if (null == instance) {
   instance = new Singleton();
  }
  return instance;
 }

 public void demoMethod() {
  System.out.println("Print demoMethod");
 }

 public static void demoStaticMethod() {
  System.out.println("Print demoStaticMethod");
 }

}

SingletonMainClass.java
package design.patern.singleton;

public class SingletonMainClass {

 public static void main(String args[]) {
  Singleton inst = Singleton.getInstance();
  System.out.println(inst.hashCode());
  inst.demoMethod();
  Singleton.demoStaticMethod();
 }
}

Write Junit test cases for singletone clas
package design.patern.singleton;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Test;

public class SingletonClassTest {

 @Test
 public void testSingleton() {
  Singleton instance1 = Singleton.getInstance();
  Singleton instance2 = Singleton.getInstance();
  System.out.println("checking singleton objects equality");
  assertEquals(true, instance1 == instance2);
 }
}

  enum based singleton example 

While we create Singleton pattern using Enum, that in only one line of code and Enum instance is thread-safe and guaranteed by JVM. This is the most easy way of writing thread-safe Singleton in Java
public enum SingletonUsingEnum{
    INSTANCE;
}

Let's see complete example and how to use.
public enum SingletonUsingEnum {
    INSTANCE;
 
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }  
}

public class SingletonEnumTest {

 public static void main(String[] args) {

  SingletonUsingEnum singletone = SingletonUsingEnum.INSTANCE;
  singletone.setName("Amby");
  System.out.println(singletone.getName());

  SingletonUsingEnum singletone2 = SingletonUsingEnum.INSTANCE;
  System.out.println("singletone: " + singletone.hashCode() + "<======>singletone2: " + singletone2.hashCode());
  System.out.println("Is same singletone reference : " + singletone.equals(singletone2));
 }
}
Output: As we can see. I have created two instance of singleton but its return same hashCode() and also return true by using equal methods on two different reference.
Amby
singletone: 2018699554<======>singletone2: 2018699554
Is same singletone reference : true

Thread Safe Singleton in Java
package design.patern.singleton;

public class Singleton {

 private static Singleton instance = null;
 private static Object staticObject = new Object();

 // Constructor should be private
 private Singleton() {
 }

 // Create instance of class
 public static Singleton getInstance() {
  Singleton result = instance;
  if (result == null) {
   synchronized (staticObject) {
    result = instance;
    if (result == null)
     instance = result = new Singleton();
   }
  }
  return result;
 }

 public void demoMethod() {
  System.out.println("Print demoMethod");
 }

 public static void demoStaticMethod() {
  System.out.println("Print demoStaticMethod");
 }

}

Q- How to prevent Singleton Class from Serialization, Cloning and Reflection?
Answer:- Prevent Singleton Class from Serialization, Cloning and Reflection

Q:- Singleton pattern Vs static class?
Answer:- Singleton pattern Vs static class

Question : Difference between singleton class and singleton scope in spring ? 

Spring Singleton Scope : Singleton in Spring guarantees to create only one bean instance for given bean per id definition per container.  that means if we define same bean with two id or name than two instance world be created. Spring Singletone Scope Example

Spring singleton pattern is different from gang of four singleton pattern. While gang of four Singleton pattern ensures that one and only one instance is created per ClassLoader.

No comments:

Post a Comment