The Java class which restricts object creation of it as single object is called singleton class and also singleton design pattern.
Singleton pattern mainly used in such a cases , where we can create single instance that can be used in entire application. Example Logging object that can be accessed throughout application. In java api we have singleton classes java.lang.Runtime and java.awt.Desktop.
Singleton implementations
1. We have to take private final instance of the singleton class which is not accessible from outside classes .
2. We have to write private constructor for object creation in the same class and not in outside classes.
3. We have to write public static method which returns singleton object and accessible from out side.
There are so many ways to create singleton object
Early Initialization of Singleton object
In this case the singleton class object will be created at the time of class loading and it is the simple way of object creation. But the problem with this is that the object will be created even the application don't use the singleton object. Here is the example
public class earlyInitialization {
private static final earlyInitialization singletonObject = new earlyInitialization();
private earlyInitialization(){
}
public static earlyInitialization getInstance(){
return singletonObject;
}
}
Lazy Initialization
In lazy initialization the object creation will be done only when its called. And this approach is good for single thread application. Why because in multi threaded applications there is a chance of more than one object creation.Here is the example.
public class lazyInitialization {
private static lazyInitialization singletonObject;
private lazyInitialization(){
}
public static lazyInitialization getInstance(){
if(singletonObject == null)
singletonObject = new lazyInitialization();
return singletonObject;
}
}
Thread safe Initialization
In this approach we can create singleton object with thread safe by using synchronization , but performance will be reduced because it will allow one thread at a time to access the getInstance() method.
public class threadSafeInitialization {
private static threadSafeInitialization singletonObject;
private threadSafeInitialization(){
}
public static synchronized threadSafeInitialization getInstance(){
if(singletonObject != null)
singletonObject = new threadSafeInitialization();
return singletonObject;
}
}
To increase the performance a bit we better use synchronized block as below
public class threadSafeInitialization {
private static threadSafeInitialization singletonObject;
private threadSafeInitialization(){
}
public static threadSafeInitialization getInstance(){
synchronized(threadSafeInitialization.class){
if(singletonObject != null)
singletonObject = new threadSafeInitialization();
return singletonObject;
}
}
}
To provide more thread safety we will use Inner static classes
Singleton pattern mainly used in such a cases , where we can create single instance that can be used in entire application. Example Logging object that can be accessed throughout application. In java api we have singleton classes java.lang.Runtime and java.awt.Desktop.
Singleton implementations
1. We have to take private final instance of the singleton class which is not accessible from outside classes .
2. We have to write private constructor for object creation in the same class and not in outside classes.
3. We have to write public static method which returns singleton object and accessible from out side.
There are so many ways to create singleton object
Early Initialization of Singleton object
In this case the singleton class object will be created at the time of class loading and it is the simple way of object creation. But the problem with this is that the object will be created even the application don't use the singleton object. Here is the example
public class earlyInitialization {
private static final earlyInitialization singletonObject = new earlyInitialization();
private earlyInitialization(){
}
public static earlyInitialization getInstance(){
return singletonObject;
}
}
Lazy Initialization
In lazy initialization the object creation will be done only when its called. And this approach is good for single thread application. Why because in multi threaded applications there is a chance of more than one object creation.Here is the example.
public class lazyInitialization {
private static lazyInitialization singletonObject;
private lazyInitialization(){
}
public static lazyInitialization getInstance(){
if(singletonObject == null)
singletonObject = new lazyInitialization();
return singletonObject;
}
}
Thread safe Initialization
In this approach we can create singleton object with thread safe by using synchronization , but performance will be reduced because it will allow one thread at a time to access the getInstance() method.
public class threadSafeInitialization {
private static threadSafeInitialization singletonObject;
private threadSafeInitialization(){
}
public static synchronized threadSafeInitialization getInstance(){
if(singletonObject != null)
singletonObject = new threadSafeInitialization();
return singletonObject;
}
}
To increase the performance a bit we better use synchronized block as below
public class threadSafeInitialization {
private static threadSafeInitialization singletonObject;
private threadSafeInitialization(){
}
public static threadSafeInitialization getInstance(){
synchronized(threadSafeInitialization.class){
if(singletonObject != null)
singletonObject = new threadSafeInitialization();
return singletonObject;
}
}
}
To provide more thread safety we will use Inner static classes
public class innerStaticClass {
private static innerStaticClass singletonObject;
private innerStaticClass(){
}
private static class innerClass{
private static final innerStaticClass Instance = new innerStaticClass();
}
public static innerStaticClass getInstance(){
return innerClass.Instance;
}
}
Using Enum to create singleton object
I feel Enum is the best thread safety for singleton object creation.
public enumSingleton{
singletonInstance;
public static void methodX(){
//our implementation
}
}
No comments:
Post a Comment