프로 그래 밍 의 자바 디자인 모드(단일 모드)

1.일반 사례 모델
이러한 결함:다 중 스 레 드 문제 에 대응 할 수 없습니다.
package com.boonya.pattern.singleton;
/**
 *       
 * <li>    : Singleton</li> 
 * <li>    : $     </li> 
 * <li>    :      </li>
 * <li>    :2013-9-27</li> 
 * <li>    :  [email protected]</li>
 * <li>    :           ,         </li>
 */
public class Singleton
{
	/**
	 *            
	 */
    private  static  Singleton singleton=new Singleton();
	
	/**
	 *          ,   new     
	 */
	private Singleton(){
		System.out.println("singleton constructor");
	}
	
	/**
	 *       ,               synchronized             
	 */
	public static  /*synchronized*/  Singleton  getInstance(){
		return singleton;
	}

}

2.게 으 른 로 딩 단일 모드
이러한 결함:다 중 스 레 드 를 사용 할 때 시스템 성능 이 현저히 떨 어 집 니 다.
package com.boonya.pattern.singleton;
/**
 *       
 * <li>    : LazySingleton</li> 
 * <li>    : $     </li> 
 * <li>    :      </li>
 * <li>    :2013-9-27</li> 
 * <li>    :  [email protected]</li>
 * <li>    :           ,         </li>
 */
public class LazySingleton
{
	/**
	 *          
	 */
	private  static  LazySingleton singleton=null;
	
	/**
	 *          ,   new     
	 */
	private LazySingleton(){
		System.out.println("singleton constructor");
	}
	
	/**
	 *       ,            (           null)
	 */
	public static  synchronized  LazySingleton  getInstance(){
		if(singleton==null){
			singleton=new LazySingleton();
		}
		return singleton;
	}

}

3.강화(plus)사례
이러한 결함:반사 체 제 를 사용 할 때 단일 사례 류 의 개인 구조 기 를 강제로 호출 하여 여러 개의 인 스 턴 스 를 생 성 합 니 다.
package com.boonya.pattern.singleton;
/**
 *       
 * <li>    : PlusSingleton</li> 
 * <li>    : $     </li> 
 * <li>    :      </li>
 * <li>    :2013-9-27</li> 
 * <li>    :  [email protected]</li>
 * <li>    :           ,         </li>
 */
public class PlusSingleton
{
	/**
	 *          ,   new     
	 */
	private  PlusSingleton(){
		System.out.println("singleton constructor"); 
	}
	
	/**
	 *    JVM     
	 */
	private static class SingletonObject{
		private  static  PlusSingleton singleton=new PlusSingleton();
	}
	
	/**
	 *           
	 */
	public static PlusSingleton  getInstance(){
		return SingletonObject.singleton;
	}

}

4.직렬 화 사례
이러한 결함:직렬 화 와 반 직렬 화 는 단일 사례 를 파괴 할 수 있다.
package com.boonya.pattern.singleton;

import java.io.Serializable;
/**
 *       
 * <li>    : Singleton</li> 
 * <li>    : $     </li> 
 * <li>    :      </li>
 * <li>    :2013-9-27</li> 
 * <li>    :  [email protected]</li>
 * <li>    :           ,                    </li>
 */
@SuppressWarnings("serial")
public class SerializableSingleton implements Serializable
{
	private  String context;
	
	public String getContext()
	{
		return context;
	}

	public void setContext(String context)
	{
		this.context = context;
	}

	/**
	 *            
	 */
    private  static  SerializableSingleton instance=new SerializableSingleton();
	
	/**
	 *          ,   new     
	 */
	private SerializableSingleton(){
		System.out.println("singleton constructor");
		context="SerializableSingleton";
	}
	
	/**
	 *       
	 */
	public static  SerializableSingleton  getInstance(){
		return instance;
	}
	
	/**
	 *         ,        (                    )
	 */
	private  Object  readResolve(){
		return instance;
	}

}

5.성능 및 주의사항
     (1)자주 사용 하 는 대상 과 값 이 변 하지 않 습 니 다.구성 요소 와 같은 대상 은 하나의 예 를 사용 하 는 것 을 권장 합 니 다.게 으 른 로 딩 방식 을 사용 하 는 것 을 권장 합 니 다.사용 할 때 다시 예화 하여 JVM 이 static 를 초기 화 할 때 new 예화 대상 이 성능 을 소모 하고 GC 메커니즘 의 실행 을 줄 이 는 것 을 권장 합 니 다.
    (2)다 중 스 레 드 에서 사용 할 지 여 부 를 고려 하여 다 중 스 레 드 에서 단일 소모 성능 이 심각 합 니 다.
    (3)단 례 를 사용 할 때 되도록 서열 화 조작 을 사용 하지 마라.위험 은 예측 하기 어렵 고 구체 적 인 문 제 를 구체 적 으로 분석 하 는 것 이 좋다.

좋은 웹페이지 즐겨찾기