Introduction
In this tutorial we will lean about singleton design pattern in java with example. In a singleton design pattern only one object can be created for one JVM. Singleton object can be accessible through out the application which is called as global accessing model. There are different ways to create Singleton Design Pattern in java. Before knowing different ways of singleton design pattern implementation, first we will see what are basic components of a singleton class which produces only one object.Components of Singleton class
To create a singleton class , a class must have the following components.- private constructor
- private ans static instance of the same class
- public and static instance method which returns an object of the class
Types of Singleton classes based on object creation time
Mainly there are two cases to create objects, one is creating singleton object at startup of application and another one is after startup whenever required, which are defined as bellow- Early initialization ( at the time of application startup object will be created )
- Lazy initialization ( whenever required object will be created once application started )
Early initialization based singleton class
As we said in the components section (in the above ) we have to create a class with three componentspublic class StoreInfo { // private instance private static StoreInfo si = new StoreInfo(); // private constructor private StoreInfo() { } // method to return object public static StoreInfo getStoreInfoObject() { return si; } }This model of singleton pattern initialise object at the time of runtime of application. So you have to decide whether an object need to be created at runtime or not, if not required, and if you want to create a singleton object whenever required after runtime then we need to create an object with lazy initialization model of singleton object creation.
Pros | Cons |
---|---|
Simple to crate |
|
Lazy initialization based singleton class
In this section we will see how to create singleton class with lazy initialization model. Remember any type of singleton class must contain the above three components( private and static instance of same class, private constructor and public, static method which returns same class object ).public class StoreInfoLazy { // private and static instance private static StoreInfoLazy sil; //private constructor private StoreInfoLazy() { } //method returns singleton class object public static StoreInfoLazy getStoreInfoLazy() { if (sil == null) { sil = new StoreInfoLazy(); } return sil; } }In the above code you can notice that object has been created in method body only.
Static block based singleton class creation
This mechanism supports exception handling in early singleton pattern method. Following is the example with static block which creates object and supports exception handling.public class StoreInfoStaticBlock { // private instance reference variable private static StoreInfoStaticBlock sisb; // private constructor private StoreInfoStaticBlock() { }; // static block with object creation and exception handling static { try { if (sisb == null) { sisb = new StoreInfoStaticBlock(); } } catch (Exception e) { throw e; } } public static StoreInfoStaticBlock getStoreInfoStaticBlock() { return sisb; } }
Bill Pugh Singleton Implementation
Main behaviour of singleton method pattern class is: having only one instance and accessing that object globally, but not initiating object at runtime.Bill pugh depends on java inner class technique. See the following example where we are going to create an inner helper class for a class which object need to be only one and global accessing.
public class BillPughSingleTonClass { // private constructor private BillPughSingleTonClass() { } // inner class - this is a helper class to create an object private static class SingleTonClassHelper { private static final BillPughSingleTonClass bpso = new BillPughSingleTonClass(); } // private method which returns singleton class object public static BillPughSingleTonClass getBillPughSingleTonClass() { return SingleTonClassHelper.bpso; } }This type of singleton class creation allows you to access the object whenever you required.
Eager initialization and Lazy initialization works fine in a single threaded environment. It won’t be suitable in multi threaded environment.
Static Block Initialization has one drawback. If we have a class with 3 static fields and application needs to access only 1, for which, instance creation is not required at all, we still have one instance created through whether we require it or not.
Thread Safe Singleton works fine in the multi threaded environment but reduces the performance because of the cost associated with the synchronized method.
Static Block Initialization has one drawback. If we have a class with 3 static fields and application needs to access only 1, for which, instance creation is not required at all, we still have one instance created through whether we require it or not.
Thread Safe Singleton works fine in the multi threaded environment but reduces the performance because of the cost associated with the synchronized method.
To overcome the issue of synchronization, Bill Pugh came up with his implementation using the static inner helper class. The static inner class is not loaded into memory until its getInstance() method is called.
Post a Comment
Post a Comment