반응형
틀린점이나 보충할 사항은 항상 수정하겠습니다.
싱글턴 패턴은 언제 사용하는 것일까???
오직 하나의 클래스 인스턴스만 공유되어야 할 때 사용했었다.
static 으로 유일한 변수, 메소드를 공유할 수 있다.
class의 모든 것을 하나만 공유하는 방법은 없을까?? 이것이 바로 singleTon이다.
public class test {
public static void main(String args[]){
Sing s=Sing.getInstance();
System.out.println(s.num);
s.num+=5;
Sing s2=Sing.getInstance();
System.out.println(s2.num);
}
}
class Sing{
int num=99;
private static Sing s;
static Sing getInstance(){
if(s==null)
s=new Sing();
return s;
}
}
반응형