단일 모드 (8 가지 실현 방식)

6307 단어 디자인 모드
단일 모드 8 가지 실현 방식
 
1. 굶 주 린 사람
/**
 *     
 *   :  、    
 *   :    (               ,         )
 */
public class HungraySingleton {
    //     ,          
    private static final HungraySingleton instance = new HungraySingleton();
    //     
    public HungraySingleton() {
    }
    //       
    public final static HungraySingleton getInstance(){
        return instance;
    }
}

2、 굶 주 린 한 예 - 정적 코드 블록 (설치)
/**
 *        
 */
public class HungrayStaticSingleton {

    private static final HungrayStaticSingleton instance;

    static {
        instance = new HungrayStaticSingleton();
    }

    public HungrayStaticSingleton() {
    }

    public static HungrayStaticSingleton getInstance(){
        return instance;
    }
}

 3、 게으름뱅이
/**
 *    
 *   :    ,    
 *   :     
 */
public class LazySingleton {

    private static LazySingleton instance;

    public LazySingleton() {
    }
    /**
     *    synchronized         ,       
     */
    public synchronized static final LazySingleton getInstance(){
        if (instance == null){
            instance = new LazySingleton();
        }
        return instance;
    }
}

4. 게으름뱅이 일례 - 이중 검사 자물쇠 
/**
 *        
 *   :   ,    ,      
 *   :   
 *
 */
public class LazyDoubleCheckSingleton {

    private volatile static LazyDoubleCheckSingleton instance;

    public LazyDoubleCheckSingleton() {
    }

    public static final LazyDoubleCheckSingleton getInstance() {
        //            ,       
        if (instance == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                //        
                if (instance == null) {
                    instance = new LazyDoubleCheckSingleton();
                    //            volatile   
                }
            }
        }
        return instance;
    }
}

5. 게으름뱅이 - 정적 내부 클래스
**
 *   ,     
 *   :   ,    ,      
 *   :    
 */
public class LazyStaticInnerClassSingleton {

    //      Java   ,        ,                   ,
    //          ,        
    public LazyStaticInnerClassSingleton() {
        //                ,           
        if (LazyHoldler.instance != null) {
            throw new RuntimeException("          ");
        }
    }

    public static final LazyStaticInnerClassSingleton getInstance() {
        return LazyHoldler.instance;
    }

    private final static class LazyHoldler {
        private static final LazyStaticInnerClassSingleton instance = new LazyStaticInnerClassSingleton();
    }
} 

6. 매 거 사례 (가장 우아 한 단일 사례 실현 모델)
/**
 *            
 * **************************         ******************************
 *                 。       , JVM                  。
 */
public enum EnumSingleton {
    INSTANCE;
    private String s = null;

    private EnumSingleton() {
        s = new String();
    }

    public String getInstance() {
        return s;
    }
}

7. 등록 식 사례 
/**
 *      ,         
 * Spring    ,         
 */
public class ContainerSingleton {

    public ContainerSingleton() {
    }

    private static Map ioc = new ConcurrentHashMap();

    public static Object getInstance(String className) {
        //            
        if (!ioc.containsKey(className)) {
            synchronized (ioc) {
                //      
                if (!ioc.containsKey(className)) {
                    Object obj = null;
                    try {
                        obj = Class.forName(className).newInstance();
                        ioc.put(className, obj);
                    } catch (Exception e) {

                    }
                    return obj;
                } else {
                    return ioc.get(className);
                }
            }
        }
        return ioc.get(className);
    }
}

8. threadLocal 실현 사례 
/**
 * ThreadLocal       
 *           ;
 *         ;
 *               ;
 *               ;
 */
public class ThreadLocalSingleton {
    private static final ThreadLocal threadLocalInstanceThreadLocal
            = new ThreadLocal() {
        @Override
        protected ThreadLocalSingleton initialValue() {
            return new ThreadLocalSingleton();
        }
    };

    private ThreadLocalSingleton() {
    }

    public static ThreadLocalSingleton getInstance() {
        return threadLocalInstanceThreadLocal.get();
    }
}
public class ThreadLocalRunnable implements Runnable {
    public void run() {
        ThreadLocalSingleton instance =ThreadLocalSingleton.getInstance();
        System.out.println(Thread.currentThread().getName() + " : " + instance);
    }
}
ThreadLocalSingleton instance = ThreadLocalSingleton.getInstance();
System.out.println(Thread.currentThread().getName() + " : " + instance);
instance = ThreadLocalSingleton.getInstance();
System.out.println(Thread.currentThread().getName() + " : " + instance);
instance = ThreadLocalSingleton.getInstance();
System.out.println(Thread.currentThread().getName() + " : " + instance);
instance = ThreadLocalSingleton.getInstance();
System.out.println(Thread.currentThread().getName() + " : " + instance);
instance = ThreadLocalSingleton.getInstance();
System.out.println(Thread.currentThread().getName() + " : " + instance);

Thread t1 = new Thread(new ThreadLocalRunnable());
Thread t2 = new Thread(new ThreadLocalRunnable());

t1.start();
t2.start();

System.out.println("Program End");

실행 결과
main : com.pa.designMode.singleton.ThreadLocalSingleton@65ab7765 main : com.pa.designMode.singleton.ThreadLocalSingleton@65ab7765 main : com.pa.designMode.singleton.ThreadLocalSingleton@65ab7765 main : com.pa.designMode.singleton.ThreadLocalSingleton@65ab7765 main : com.pa.designMode.singleton.ThreadLocalSingleton@65ab7765 Program End Thread-0 : com.pa.designMode.singleton.ThreadLocalSingleton@2770f418 Thread-1 : com.pa.designMode.singleton.ThreadLocalSingleton@dc7a4fc
 
 
 

좋은 웹페이지 즐겨찾기