From name Singleton, this pattern restricts the object instantiate and ensures it have only one instance of class is available in JVM and it has below characteristics.
1. Singleton pattern object instantiate and ensures it have only one instance of class is available in JVM.
2. Class should have global access to provide the instance of object.
3. Most commonly used singleton pattern is for Logging in java application.
We have different approaches to implement singleton design pattern and should follow below common points.
1. Constructor should be private so that we other classes can not create object using constructor.
2. Class should have one public static variable to hold the instance of class.
3. Class should have public static method to return the object of class. Application should get the instance of class by using this method. Hence this method should have global access across application.
Singleton Early initiation:
In this approach object of class in created when it is loaded even client is not request for this object.
package com.sample.java.testing;
public class SingletonEagerInitiationTest {
public static SingletonEagerInitiationTest eagerInitiationTest = new SingletonEagerInitiationTest();
private SingletonEagerInitiationTest() {
}
public static SingletonEagerInitiationTest getInstance() {
return eagerInitiationTest;
}
}
But generally we should avoid object creation early until unless some other classes required it. And also we do not perform any exception handling.
Static block initiation
In this approach we can provide object creation using static block with exception handling.
package com.sample.java.testing;
public class SingletonStaticInitiationTest {
public static SingletonStaticInitiationTest initiationTest;
private SingletonStaticInitiationTest(){
}
static {
try {
initiationTest = new SingletonStaticInitiationTest();
} catch (Exception e) {
System.out.println("failed to initialize object" + e);
}
}
public static SingletonStaticInitiationTest getInstance() {
return initiationTest;
}
}
In this approach also object will initialize early even any other classes are not accessed to this object. We can void this by using Lazy Initiation.
In lazy initiation approach, it won't create object while class loading. It will create object and returns when any other class accessed getInstance() method.
package com.sample.java.testing;
public class SingletonLazyInitiationTest {
public static SingletonLazyInitiationTest singletonLazyInitiationTest;
private SingletonLazyInitiationTest() {
}
public static SingletonLazyInitiationTest getInstance() {
try {
if (singletonLazyInitiationTest == null) {
singletonLazyInitiationTest = new SingletonLazyInitiationTest();
return singletonLazyInitiationTest;
} else {
return singletonLazyInitiationTest;
}
} catch (Exception e) {
System.out.println("failed to initialize object" + e);
return null;
}
}
}
Here if we are using multiple threads, and two different threads are trying to access the
getInstance() method, it will breaks the singleton pattern. Hence we should follow below approach in multi thread environment by making getInstance() method as synchronized.
package com.sample.java.testing;
public class SingletonSyncronizedIntitiationTest {
public static SingletonSyncronizedIntitiationTest singletonSyncronizedIntitiationTest;
private SingletonSyncronizedIntitiationTest() {
}
public static synchronized SingletonSyncronizedIntitiationTest getInstance() {
try {
if (singletonSyncronizedIntitiationTest == null) {
singletonSyncronizedIntitiationTest = new SingletonSyncronizedIntitiationTest();
return singletonSyncronizedIntitiationTest;
} else {
return singletonSyncronizedIntitiationTest;
}
} catch (Exception e) {
System.out.println("failed to initialize object" + e);
return null;
}
}
}
Here even if multiple threads can not get access to getInstance() method as it is synchronized. But it will impact on performance.
1. Singleton pattern object instantiate and ensures it have only one instance of class is available in JVM.
2. Class should have global access to provide the instance of object.
3. Most commonly used singleton pattern is for Logging in java application.
We have different approaches to implement singleton design pattern and should follow below common points.
1. Constructor should be private so that we other classes can not create object using constructor.
2. Class should have one public static variable to hold the instance of class.
3. Class should have public static method to return the object of class. Application should get the instance of class by using this method. Hence this method should have global access across application.
Singleton Early initiation:
In this approach object of class in created when it is loaded even client is not request for this object.
package com.sample.java.testing;
public class SingletonEagerInitiationTest {
public static SingletonEagerInitiationTest eagerInitiationTest = new SingletonEagerInitiationTest();
private SingletonEagerInitiationTest() {
}
public static SingletonEagerInitiationTest getInstance() {
return eagerInitiationTest;
}
}
But generally we should avoid object creation early until unless some other classes required it. And also we do not perform any exception handling.
Static block initiation
In this approach we can provide object creation using static block with exception handling.
package com.sample.java.testing;
public class SingletonStaticInitiationTest {
public static SingletonStaticInitiationTest initiationTest;
private SingletonStaticInitiationTest(){
}
static {
try {
initiationTest = new SingletonStaticInitiationTest();
} catch (Exception e) {
System.out.println("failed to initialize object" + e);
}
}
public static SingletonStaticInitiationTest getInstance() {
return initiationTest;
}
}
In this approach also object will initialize early even any other classes are not accessed to this object. We can void this by using Lazy Initiation.
In lazy initiation approach, it won't create object while class loading. It will create object and returns when any other class accessed getInstance() method.
package com.sample.java.testing;
public class SingletonLazyInitiationTest {
public static SingletonLazyInitiationTest singletonLazyInitiationTest;
private SingletonLazyInitiationTest() {
}
public static SingletonLazyInitiationTest getInstance() {
try {
if (singletonLazyInitiationTest == null) {
singletonLazyInitiationTest = new SingletonLazyInitiationTest();
return singletonLazyInitiationTest;
} else {
return singletonLazyInitiationTest;
}
} catch (Exception e) {
System.out.println("failed to initialize object" + e);
return null;
}
}
}
Here if we are using multiple threads, and two different threads are trying to access the
getInstance() method, it will breaks the singleton pattern. Hence we should follow below approach in multi thread environment by making getInstance() method as synchronized.
package com.sample.java.testing;
public class SingletonSyncronizedIntitiationTest {
public static SingletonSyncronizedIntitiationTest singletonSyncronizedIntitiationTest;
private SingletonSyncronizedIntitiationTest() {
}
public static synchronized SingletonSyncronizedIntitiationTest getInstance() {
try {
if (singletonSyncronizedIntitiationTest == null) {
singletonSyncronizedIntitiationTest = new SingletonSyncronizedIntitiationTest();
return singletonSyncronizedIntitiationTest;
} else {
return singletonSyncronizedIntitiationTest;
}
} catch (Exception e) {
System.out.println("failed to initialize object" + e);
return null;
}
}
}
Here even if multiple threads can not get access to getInstance() method as it is synchronized. But it will impact on performance.
No comments:
Post a Comment