Contents [hide ]
What is Singleton Design Pattern?
Singleton is a design pattern that ensures that a class can only have one object.
Singleton Class Using Eager Initialization Method
package com.hareeshsoni.singletonexample; /** * @author HAREESH * */ public class SingletonExample { // Instance will be created at load time private static SingletonExample singletonExample = new SingletonExample(); // Creating private constructor private SingletonExample() { } // Declaring static method public static SingletonExample getInstance() { return singletonExample; } }
Test above SingletonExample here.
package com.hareeshsoni.singletonexample; /** * @author HAREESH * */ public class Main { public static void main(String[] args) { SingletonExample singletonExampleObject1 = SingletonExample.getInstance(); SingletonExample singletonExampleObject2 = SingletonExample.getInstance(); SingletonExample singletonExampleObject3 = SingletonExample.getInstance(); // We will check here above created objects are equal or not if (singletonExampleObject1 == singletonExampleObject2 && singletonExampleObject2 == singletonExampleObject3) { System.out.println("Objects are same."); } else { System.out.println("Objects are different."); } } }
Singleton Class Using Lagy Initialization Method
package com.hareeshsoni.singletonexample; /** * @author HAREESH * */ public class SingletonUsingLazyInitialization { // Declaring Static variable reference private static SingletonUsingLazyInitialization singletonUsingLazyInitialization = null; // Creating private constructor private SingletonUsingLazyInitialization() { } // Declaring static method public static SingletonUsingLazyInitialization getInstance() { if(singletonUsingLazyInitialization == null) { return singletonUsingLazyInitialization = new SingletonUsingLazyInitialization(); } return singletonUsingLazyInitialization; } }
Test above Singleton Using Lazy Initialization Example here.
package com.hareeshsoni.singletonexample; /** * @author HAREESH * */ public class SingletonUsingLazyInitializationMain { public static void main(String[] args) { SingletonUsingLazyInitialization object1 = SingletonUsingLazyInitialization.getInstance(); SingletonUsingLazyInitialization object2 = SingletonUsingLazyInitialization.getInstance(); SingletonUsingLazyInitialization object3 = SingletonUsingLazyInitialization.getInstance(); // We will check here above created objects are equal or not if (object1 == object2 && object2 == object3) { System.out.println("Objects are same."); } else { System.out.println("Objects are different."); } } }
Singleton Class Using Thread Safe Method
package com.hareeshsoni.singletonexample; /** * @author HAREESH * */ public class SingletonUsingThreadSafe { // Declaring Static variable reference private static SingletonUsingThreadSafe singletonUsingThreadSafe = null; // Creating private constructor private SingletonUsingThreadSafe() { } // Declaring static synchronized method public static synchronized SingletonUsingThreadSafe getInstance() { if(singletonUsingThreadSafe == null) { return singletonUsingThreadSafe = new SingletonUsingThreadSafe(); } return singletonUsingThreadSafe; } }
Test above Singleton Using Thread Safe Example here.
package com.hareeshsoni.singletonexample; /** * @author HAREESH * */ public class SingletonUsingThreadSafeMain { public static void main(String[] args) { SingletonUsingThreadSafe object1 = SingletonUsingThreadSafe.getInstance(); SingletonUsingThreadSafe object2 = SingletonUsingThreadSafe.getInstance(); SingletonUsingThreadSafe object3 = SingletonUsingThreadSafe.getInstance(); // We will check here above created objects are equal or not if (object1 == object2 && object2 == object3) { System.out.println("Objects are same."); } else { System.out.println("Objects are different."); } } }