1. Singleton means sharing only one instance of a class in whole application, Singleton is a behavior of an instance of a class to extremely extract implementing polymorphic concept. Below example show only one instance will created at class load time and it's declared as final so there is no way to create another one this is default thread safe singleton class and recommended by most of the java experts.
public class TestSingleton {
private static final TestSinglton TEST_SINGLETON = new TestSinglton();
private TestSingleton () {
// Stop Polymorphic behaviour
}
public static TestSingleton getInsSingleton ()
{
return TEST_SINGLETON;
}
}
2. Another way of creating single instance of a class, Instance will be created only once whenever it required this is also called laze initialization
public class TestSingleton {
private static TestSingleton TEST_SINGLETON = null;
private TestSinglton() {
// Stop Polymorphic behaviour
}
public static TestSingleton getInsTestSingleton () {
if(TEST_SINGLETON == null){
TEST_SINGLETON = new TestSingleton ();
}
return TEST_SINGLETON;
}
}
3. This is not thread safe so programmer have to make explicitly thread safe
public class TestSingleton {
private static TestSingleton TEST_SINGLETON = null;
private TestSingleton () {
// Stop Polymorphic behaviour
}
public static TestSingleton getInsTestSingleton () {
if (TEST_SINGLETON == null) {
synchronized (TestSingleton.class) {
TEST_SINGLETON = new TestSingleton ();
}
}
return TEST_SINGLETON;
}
}
4. Still clone of Singleton class can be create and a duplicate instance can be obtained to resolve this problem need to override clone() method of object class see example
public class TestSingleton {
private static TestSingleton TEST_SINGLETON = null;
private TestSingleton() {
// Stop Polymorphic behaviour
}
public static TestSingleton getInsTestSingleton() {
if (TEST_SINGLETON == null) {
synchronized (TestSingleton.class) {
TEST_SINGLETON = new TestSingleton();
}
System.out.println(" I am created only once");
}
return TEST_SINGLETON;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public static void main(String[] args) {
TestSingleton.getInsTestSingleton();
}
}
5. Out put
I am created only once
NOTE: One more important things is to notice here i am calling getInsTestSingleton() method five times and out put is same see example
public class TestSingleton {
private static TestSingleton TEST_SINGLETON = null;
private TestSingleton() {
// Stop Polymorphic behaviour
}
public static TestSingleton getInsTestSingleton() {
if (TEST_SINGLETON == null) {
synchronized (TestSinglton.class) {
TEST_SINGLETON = new TestSingleton();
}
System.out.println(" I am created only once");
}
return TEST_SINGLETON ;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public static void main(String[] args) {
TestSingleton.getInsTestSingleton();//1
TestSingleton.getInsTestSingleton();//2
TestSingleton.getInsTestSingleton();//3
TestSingleton.getInsTestSingleton();//4
TestSingleton.getInsTestSingleton();//5
}
}
6. Out put
I am created only once
Hope this will useful.
Thank You
No comments:
Post a Comment