Singleton Design Pattern
We have various ways to create singleton class in java, first of all what is singleton and why it is required?
Singleton class will restrict you by creating a new object using new keyword, It provides the same single instance for the single JVM instance. If you want some introduction about design patterns please have a look it my post here.This post is little bit too long, so please hold on your breath.
So the first step is to know what wikipedia says,
Singleton class will restrict you by creating a new object using new keyword, It provides the same single instance for the single JVM instance. If you want some introduction about design patterns please have a look it my post here.This post is little bit too long, so please hold on your breath.
So the first step is to know what wikipedia says,
The singleton pattern is a software design pattern that restricts the instantiation of a class to one.Second step is to map singleton pattern to the real world example,
Each country will have a single President post, for every some years (normally 5) the people for the post will change but the country will hold only one President post.There are 5 ways we can design singleton pattern,
- Eager Initialization
- Lazy Initialization
- Lazy Initialization with Multi-thread Support
- Lazy with Double Lock Method
- Bill Pugh Singleton
- Make constructor private.
- Make a private static variable to old the object of the singleton class .
- Provide a public static method to return that instance variable.
Eager Initialization:
This type will create an instance when the JVM loads the class, even if the client doesn't use this singleton class.
Lazy Initialization:
Here, static instance variable doesn't initialize at initially, Static Instance method getInstance() will check if the static variable has null, if null it will create and return object, otherwise it will return existing object.
Lazy Initialization with Multi-thread Support:
Above two methods are good for single thread, but to support multithread we need to use keyword synchronized, here one thread will have to wait for another thread to execute that synchronized block.
Lazy with Double Lock Method:
Here, we run into a problem. Suppose that there are two threads running. Both can get inside of the if statement concurrently when the instance is null.
Then, one thread enters the synchronized block to initialize the instance, while the other is blocked. When the first thread exits in the synchronized block, the waiting thread enters and creates another singleton object.
Note that when the second thread enters the synchronized block, it does not check to see if the instance is non-null.
Bill Pugh Singleton:
This one is based on the initialization on demand idiom, What wiki says it is based on the Java Language Specification(JLS), Java Virtual Machine loads static data-members only on-demand.
Object creation will happen only when we invoke getInstance() method. JLS guaranteed the sequential execution of the class initialization, that means thread-safe. So, we actually do not need to provide explicit synchronization on static getInstance() method for loading and initialization.
Thanks for reading the post, ciao until next post.
But how Bill Pugh is best when compared with Bloch's Enum way of creating Singleton?
ReplyDelete