4중 방식 단례 모델 실현

2077 단어
굶주린 사람
package com.zkn.newlearn.gof.singleton;

/**
 *
 *  
 * @author zkn
 *
 */

public class SingletonTest01{

	/**
	 *  , , 。 
	 */
	private static SingletonTest01 singleton = new SingletonTest01();
	
	/**
	 *   
	 */
	private SingletonTest01() {
		
	}
	/**
	 *  
	 */
	public static SingletonTest01 getInstance(){
		
		return singleton;
	}
	
	public void test(){
		System.out.println(" ");
	}
	
}

2. 게으름뱅이
package com.zkn.newlearn.gof.singleton;

/**
 * 
 * @author zkn
 *
 */

public class SingletonTest02 {

	private static SingletonTest02 singleton;
	
	/**
	 *  
	 */
	private SingletonTest02() {
		
	}

	public static synchronized SingletonTest02 getInstance(){
		
		if(singleton == null){
			singleton = new SingletonTest02();
		}
		return singleton;
	}
	
	public void test(){
		System.out.println(" ");
	}
}

3: 정적 내부 클래스
package com.zkn.newlearn.gof.singleton;

/**
 *         
 * @author zkn
 *
 */


public class SingletonTest04 {

	private static class SingletonClassInstance{
		private static final SingletonTest04 single = new SingletonTest04(); 
	}
	
	private SingletonTest04() {
		
	}

	public static SingletonTest04 getInstance() {
		
		return SingletonClassInstance.single;
	}
	
}

4: 열거(단일 요소)
package com.zkn.newlearn.gof.singleton;

/**
 * 
 *     jvm 
 * @author zkn
 *
 */

public enum SingletonTest05 {

	/**
	 *  
	 */
	INSTANCE;
}

좋은 웹페이지 즐겨찾기