BAEL-7145

Bill Pugh Singleton implementation
This commit is contained in:
parthiv39731
2023-10-28 21:11:08 +05:30
parent 29d0e4f04c
commit 462ddc7f02
8 changed files with 206 additions and 0 deletions
@@ -0,0 +1,15 @@
package com.baledung.billpugh;
public class BillPughSingleton {
private BillPughSingleton() {
}
private static class SingletonHelper {
private static final BillPughSingleton BILL_PUGH_SINGLETON_INSTANCE = new BillPughSingleton();
}
public static BillPughSingleton getInstance() {
return SingletonHelper.BILL_PUGH_SINGLETON_INSTANCE;
}
}
@@ -0,0 +1,11 @@
package com.baledung.billpugh;
public class EagerLoadedSingleton {
private static final EagerLoadedSingleton EAGER_LOADED_SINGLETON = new EagerLoadedSingleton();
private EagerLoadedSingleton() {
}
public static EagerLoadedSingleton getInstance() {
return EAGER_LOADED_SINGLETON;
}
}
@@ -0,0 +1,15 @@
package com.baledung.billpugh;
public class LazyLoadedSingleton {
private static LazyLoadedSingleton lazyLoadedSingletonObj;
private LazyLoadedSingleton() {
}
public static LazyLoadedSingleton getInstance() {
if (null == lazyLoadedSingletonObj) {
lazyLoadedSingletonObj = new LazyLoadedSingleton();
}
return lazyLoadedSingletonObj;
}
}
@@ -0,0 +1,15 @@
package com.baledung.billpugh;
public class SynchronizedLazyLoadedSingleton {
private static SynchronizedLazyLoadedSingleton synchronizedLazyLoadedSingleton;
private SynchronizedLazyLoadedSingleton() {
}
public static synchronized SynchronizedLazyLoadedSingleton getInstance() {
if (null == synchronizedLazyLoadedSingleton) {
synchronizedLazyLoadedSingleton = new SynchronizedLazyLoadedSingleton();
}
return synchronizedLazyLoadedSingleton;
}
}