BAEL 1639: added singleton examples and test. (#3977)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
5b5386636b
commit
5f83980363
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.designpatterns.singleton.synchronization;
|
||||
|
||||
/**
|
||||
* Double-checked locking design pattern applied to a singleton.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class DclSingleton {
|
||||
|
||||
/**
|
||||
* Current instance of the singleton.
|
||||
*/
|
||||
private static volatile DclSingleton instance;
|
||||
|
||||
/**
|
||||
* Private constructor to avoid instantiation.
|
||||
*/
|
||||
private DclSingleton() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current instance of the singleton.
|
||||
*
|
||||
* @return the current instance of the singleton
|
||||
*/
|
||||
public static DclSingleton getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (DclSingleton.class) {
|
||||
if (instance == null) {
|
||||
instance = new DclSingleton();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.designpatterns.singleton.synchronization;
|
||||
|
||||
/**
|
||||
* Draconian singleton. The method to get the instance is synchronized.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class DraconianSingleton {
|
||||
|
||||
/**
|
||||
* Current instance of the singleton.
|
||||
*/
|
||||
private static DraconianSingleton instance;
|
||||
|
||||
/**
|
||||
* Private constructor to avoid instantiation.
|
||||
*/
|
||||
private DraconianSingleton() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current instance of the singleton.
|
||||
*
|
||||
* @return the current instance of the singleton
|
||||
*/
|
||||
public static synchronized DraconianSingleton getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new DraconianSingleton();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.designpatterns.singleton.synchronization;
|
||||
|
||||
/**
|
||||
* Singleton with early initialization. Inlines the singleton instance
|
||||
* initialization.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class EarlyInitSingleton {
|
||||
|
||||
/**
|
||||
* Current instance of the singleton.
|
||||
*/
|
||||
private static final EarlyInitSingleton INSTANCE = new EarlyInitSingleton();
|
||||
|
||||
/**
|
||||
* Private constructor to avoid instantiation.
|
||||
*/
|
||||
private EarlyInitSingleton() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current instance of the singleton.
|
||||
*
|
||||
* @return the current instance of the singleton
|
||||
*/
|
||||
public static EarlyInitSingleton getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.designpatterns.singleton.synchronization;
|
||||
|
||||
/**
|
||||
* Enum singleton pattern. Uses an enum to hold a reference to the singleton
|
||||
* instance.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public enum EnumSingleton {
|
||||
|
||||
/**
|
||||
* Current instance of the singleton.
|
||||
*/
|
||||
INSTANCE;
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.designpatterns.singleton.synchronization;
|
||||
|
||||
/**
|
||||
* Initialization on demand singleton pattern. Uses a nested static class to
|
||||
* hold a reference to the singleton instance.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class InitOnDemandSingleton {
|
||||
|
||||
/**
|
||||
* Holder for a singleton instance.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
private static class InstanceHolder {
|
||||
|
||||
/**
|
||||
* Current instance of the singleton.
|
||||
*/
|
||||
private static final InitOnDemandSingleton INSTANCE = new InitOnDemandSingleton();
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor to avoid instantiation.
|
||||
*/
|
||||
private InitOnDemandSingleton() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current instance of the singleton.
|
||||
*
|
||||
* @return the current instance of the singleton
|
||||
*/
|
||||
public static InitOnDemandSingleton getInstance() {
|
||||
return InstanceHolder.INSTANCE;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user