인스턴스 생성할때 어떻게 할까? --------> new MyClass();
만약 생성자가 private으로 선언돼있으면? --------> 인스턴스 생성 불가.
생성하게 하려면?? --> 인스턴스변수를 static변수로 미리 선언후 --> 요청시 인스턴스 변수를 만들어서
넘겨주는 식으로 생성해준다.
public class Singleton{
private static Singleton uniqueInstance;
private Singleton() { } // private로 선언된 생성자
public static Singleton getInstance() { //인스턴스 생성하는 메소드
if (uniqueInstance == null) { //인스턴스가 생성된 적이 있는지 확인후
uniqueInstance = new Singleton(); //생성
}
return uniqueInstance; //return
}
}
싱글턴 패턴 정의 ) 싱글턴 패턴은 해당 클래스의 인스턴스가 하나만 만들어지고,
어디서든지 그 인스턴스에 접근할수 있도록 하기 위한 패턴입니다.
싱글턴 패턴 : 클래스 다이어그램

싱글턴 패턴의 문제점
ChocolateBoiler boiler = new ChocolateBoiler.getInstance();
fill();
boil();
drain();
Thread1 | Thread2 | uniqueInstance |
public static Chocolate getInstance() | null | |
public static Chocolate getInstance() | null | |
if(uniqueInstance == null)
|
if(uniqueInstance == null) *안좋은 상황 |
null null |
uniqueInstance = new ChocolateBoiler(); return uniqueInstance;
|
uniqueInstance = new ChocolateBoiler();
return uniqueInstance; |
<object1> <object1>
<object2> ** 문제 발생! <object2> ** 문제 발생!
|
멀티스레딩 문제 해결 방법
1. synchronized 동기화 시키기.
public class Singleton{
private static Singleton uniqueInstance;
//기타 인스턴스 변수
private Singleton() {}
public static synchronized Singleton getInstance(){
if(uniqueInstance == null){
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
....
}
단점) 엄청난 속도 저하 ( 동기화 시키면 성능이 100배 정도 저하 )
2. 처음부터 인스턴스를 만들어 버리기.
public class Singleton{
public static Singleton uniqueInstance = new Singleton(); //!!
private Singleton(){ }
public static Singleton getInstance(){
return uniqueInstance;
}
}
인스턴스 생성할때 어떻게 할까? --------> new MyClass();
만약 생성자가 private으로 선언돼있으면? --------> 인스턴스 생성 불가.
생성하게 하려면?? --> 인스턴스변수를 static변수로 미리 선언후 --> 요청시 인스턴스 변수를 만들어서
넘겨주는 식으로 생성해준다.
public class Singleton{
private static Singleton uniqueInstance;
private Singleton() { } // private로 선언된 생성자
public static Singleton getInstance() { //인스턴스 생성하는 메소드
if (uniqueInstance == null) { //인스턴스가 생성된 적이 있는지 확인후
uniqueInstance = new Singleton(); //생성
}
return uniqueInstance; //return
}
}
싱글턴 패턴 정의 ) 싱글턴 패턴은 해당 클래스의 인스턴스가 하나만 만들어지고,
어디서든지 그 인스턴스에 접근할수 있도록 하기 위한 패턴입니다.
싱글턴 패턴 : 클래스 다이어그램

싱글턴 패턴의 문제점
ChocolateBoiler boiler = new ChocolateBoiler.getInstance();
fill();
boil();
drain();
Thread1 | Thread2 | uniqueInstance |
public static Chocolate getInstance() | null | |
public static Chocolate getInstance() | null | |
if(uniqueInstance == null)
|
if(uniqueInstance == null) *안좋은 상황 |
null null |
uniqueInstance = new ChocolateBoiler(); return uniqueInstance;
|
uniqueInstance = new ChocolateBoiler();
return uniqueInstance; |
<object1> <object1>
<object2> ** 문제 발생! <object2> ** 문제 발생!
|
멀티스레딩 문제 해결 방법
1. synchronized 동기화 시키기.
public class Singleton{
private static Singleton uniqueInstance;
//기타 인스턴스 변수
private Singleton() {}
public static synchronized Singleton getInstance(){
if(uniqueInstance == null){
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
....
}
단점) 엄청난 속도 저하 ( 동기화 시키면 성능이 100배 정도 저하 )
2. 처음부터 인스턴스를 만들어 버리기.
public class Singleton{
public static Singleton uniqueInstance = new Singleton(); //!!
private Singleton(){ }
public static Singleton getInstance(){
return uniqueInstance;
}
}