ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Singleton패턴
    이론 2015. 9. 18. 16:12
    반응형


    Singleton Pattern


    디자인패턴 중에 하나로

    특정 클래스의 객체가 한 개만 존재하도록

    제어하는 방법



    클래스에 Singleton패턴 적용하는 방법


    1. 자기 자신 타입의 참조변수를 static으로 등록


    ex)


    public class Singleton {


    private static Singleton instance;


    }



    --------------------------------------------------------------
    2. 생성자를 private으로 감추기

    ex)

    public class Singleton {

    private static Singleton instance;
    private Singleton(){}
    }


    --------------------------------------------------------------
    3. 1번 변수에 대한 getter를 만들 되, 해당 변수가 null이면 
       새로운 객체를 할당하고 instance의 값을 반환


    public class Singleton {

    private static Singleton instance;
    private Singleton(){}
    public static Singleton getInstance(){
    if(instance == null)
    instance = new Singleton();
    return instance;
    }
    }












    반응형

    '이론' 카테고리의 다른 글

    동적바인딩  (0) 2015.09.21
    암시적, 명시적 관계  (0) 2015.09.18
    종단클래스/메소드  (0) 2015.09.18
    상속의 관계  (0) 2015.09.18
    접근 지정자  (0) 2015.09.18
Designed by Tistory.