Java 23種類の設計モードの単例モード7種類の実現方式
6488 ワード
一、怠け者式(スレッドが安全ではない)
二、怠け者式(スレッド安全)
三、餓漢式(スレッド安全)
四、キャッシュ実装(スレッドが安全でない)
五、二重検査ロック(怠け者スレッドセキュリティのさらなる最適化)
六、Lazy Initialization Holder Class実現単例
七、列挙実現単例
八、テストクラス
九、テスト結果
package com.java.singleton;
//
public class LazySingleton {
//
private LazySingleton() {
}
private static LazySingleton instance = null;
//
public static LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
}
return instance;
}
}
二、怠け者式(スレッド安全)
package com.java.singleton;
//
public class SynchronizedLazySingleton {
//
private SynchronizedLazySingleton() {
}
private static SynchronizedLazySingleton instance = null;
//
public static synchronized SynchronizedLazySingleton getInstance() {
if (instance == null) {
instance = new SynchronizedLazySingleton();
}
return instance;
}
}
三、餓漢式(スレッド安全)
package com.java.singleton;
//
public class HungrySingleton {
//
private HungrySingleton() {
}
// JVM
private static HungrySingleton instance = new HungrySingleton();
//
public static HungrySingleton getInstance() {
return instance;
}
}
四、キャッシュ実装(スレッドが安全でない)
package com.java.singleton;
import java.util.HashMap;
import java.util.Map;
//
public class CacheSingleton {
//
private CacheSingleton() {
}
//
private static Map map = new HashMap<>();
// key
private static final String DEFAULT_KEY = "Cache";
//
public static CacheSingleton getInstance() {
//
CacheSingleton instance = (CacheSingleton) map.get(DEFAULT_KEY);
if (instance == null) {
instance = new CacheSingleton();
map.put(DEFAULT_KEY, instance);
}
return instance;
}
}
五、二重検査ロック(怠け者スレッドセキュリティのさらなる最適化)
package com.java.singleton;
// synchronized
public class DoubleCheckedLockingSingleton {
//
private DoubleCheckedLockingSingleton() {
}
//volatile
private volatile static DoubleCheckedLockingSingleton instance = null;
//
public static DoubleCheckedLockingSingleton getInstance() {
if (instance == null) {
synchronized (DoubleCheckedLockingSingleton.class) {
if (instance == null) {
instance = new DoubleCheckedLockingSingleton();
}
}
}
return instance;
}
}
六、Lazy Initialization Holder Class実現単例
package com.java.singleton;
// Lazy Initialization Holder Class
public class LazyInitializationHolderClassSingleton {
//
private LazyInitializationHolderClassSingleton() {
}
/*
*
* LazyInitializationHolder instance
*
*/
private static class LazyInitializationHolder {
/*
* JVM
*
*/
private static LazyInitializationHolderClassSingleton instance = new LazyInitializationHolderClassSingleton();
}
//
public static LazyInitializationHolderClassSingleton getInstance() {
return LazyInitializationHolder.instance;
}
}
七、列挙実現単例
package com.java.singleton;
public enum EnumSingleton {
//
uniqueEnumSingleton;
public void method() {
System.out.println("EnumSingleton"+uniqueEnumSingleton.hashCode());
}
}
八、テストクラス
package com.java.singleton;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SingletonApplicationTests {
@Test
public void testCacheSingleton() {
for (int i = 0; i < 3; i++) {
System.out.println("CacheSingleton\t" + CacheSingleton.getInstance());
}
System.out.println("-------------------------------------------------------------------
");
}
@Test
public void testDoubleCheckedLockingSingleton() {
for (int i = 0; i < 3; i++) {
System.out.println("DoubleCheckedLockingSingleton\t" + DoubleCheckedLockingSingleton.getInstance());
}
System.out.println("-------------------------------------------------------------------
");
}
@Test
public void testEnumSingleton() {
for (int i = 0; i < 3; i++) {
EnumSingleton.uniqueEnumSingleton.method();
}
System.out.println("-------------------------------------------------------------------
");
}
@Test
public void testHungrySingleton() {
for (int i = 0; i < 3; i++) {
System.out.println("HungrySingleton\t" + HungrySingleton.getInstance());
}
System.out.println("-------------------------------------------------------------------
");
}
@Test
public void testLazyInitializationHolderClassSingleton() {
for (int i = 0; i < 3; i++) {
System.out.println("LazyInitializationHolderClassSingleton\t" + LazyInitializationHolderClassSingleton.getInstance());
}
System.out.println("-------------------------------------------------------------------
");
}
@Test
public void testLazySingleton() {
for (int i = 0; i < 3; i++) {
System.out.println("LazySingleton\t" + LazySingleton.getInstance());
}
System.out.println("-------------------------------------------------------------------
");
}
@Test
public void testSynchronizedLazySingleton() {
for (int i = 0; i < 3; i++) {
System.out.println("SynchronizedLazySingleton\t" + SynchronizedLazySingleton.getInstance());
}
System.out.println("-------------------------------------------------------------------
");
}
}
九、テスト結果