Language/Java
[Design Pattern] Singleton 패턴
TechNote.kr
2020. 11. 26. 23:39
728x90
인스턴스가 하나 뿐인 객체를 만들 수 있게 해주는 패턴 |
특정 클래스에 대해 객체 인스턴스를 하나만 만들 수 있다.
사용 용도>
자원 관리 Pool, 특정 하드웨어를 Control하는 디바이스 드라이버 등
비슷한 역할을 하는 전역 변수와의 비교>
전역 변수는 애플리케이션 시작될 때 생성 불필요하게 자원을 잡아먹는 경우 발생 |
Singleton은 필요할 때 객체 생성 |
[Singleton 기본 구조]
public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if(uniqueInstance == null)
uniqueInstance = new Singleton();
return uniqueInstance;
}
}
728x90