시그마 삽질==six 시그마

싱글톤 패턴(Singleton Pattern) 본문

프로그래밍/디자인패턴

싱글톤 패턴(Singleton Pattern)

Ethan Matthew Hunt 2020. 4. 23. 23:05
public class SingleExample {
    private static SingleExample singleton = new SingleExample();
    private SingleExample() {
    }
    public static SingleExample getInstance() {
        return singleton;
    }
}

한개의 인스턴스를 생성해서 전역적으로 공유해서 사용하는 패턴임

 

1. private  static 멤버변수 객체 생성

2. private 생성자

3. static method를 통해 가져옴

 

 

필요시 생성하고 싶다면 이렇게..

public class SingleExample {

    private SingleExample() {
    }
    
    public static class SingletonBuiler{
        static final SingleExample single = new SingleExample();
        
        public static SingleExample getInstatnce(){
            return SingletonBuiler.single;
        }
    }
}
Comments