JAVA/예제
[JAVA] ch07-10. 싱글톤 기법 (singleton)
밍글링글링
2017. 8. 22. 11:40
728x90
public class Ex10 {
public static void main(String[] args){
//Singleton single = new Singleton();
Singleton single = Singleton.getInstance();
System.out.println(single);
single.hello();
single = Singleton.getInstance();
System.out.println(single);
}
}
//stateful 데이터 상태값 (도메인) state 멤버변수에 저장되는 값
//stateless 형태의 객체를 디자인할때.
class Singleton{ //stateless 일때만 사용 // 다른사람이 객체생성하지못하게 하기 위하여
private static Singleton s = new Singleton();
private Singleton(){
}
public static Singleton getInstance(){
if(s==null) s = new Singleton();
return s;
}
public void hello(){
System.out.println("hello");
}
}
728x90