프로 그래 밍 의 자바 디자인 모드(단일 모드)
이러한 결함:다 중 스 레 드 문제 에 대응 할 수 없습니다.
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)단 례 를 사용 할 때 되도록 서열 화 조작 을 사용 하지 마라.위험 은 예측 하기 어렵 고 구체 적 인 문 제 를 구체 적 으로 분석 하 는 것 이 좋다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
OS & Swift 공부 - Singleton Design PatternSingleton 싱글턴은 '특정 클래스의 인스턴스가 오직 하나임을 보장하는 객체'를 의미 싱글턴은 애플리케이션이 요청한 횟수와는 관계없이 이미 생성된 같은 인스턴스를 반환 주의할 점 → 싱글턴 디자인 패턴은 애플리...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.