즐거운프로그래밍

[자바] 싱글톤 패턴(singleton)

수수께끼 고양이 2023. 11. 14. 10:34
728x90
반응형

싱글톤(singleton)

클래스를 통해 생성할 수 있는 객체가 한개만 되도록 만드는 것

class Singleton {
    private static Singleton one;
    private Singleton() {
    }
    public static Singleton getInstance() {
        if(one ==null) {
            one = new Singleton();
        }
        return one;
    }
}
public class Main {
    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton1 == singleton2);
    }
}

 

 

 

 

728x90
반응형

'즐거운프로그래밍' 카테고리의 다른 글

[자바] 스레드(Thread)  (1) 2023.11.14
[자바] 예외(Exception)  (0) 2023.11.14
[자바] 다형성(polymorphism)  (0) 2023.11.13
[자바] 인터페이스(interface)  (0) 2023.11.13
[자바] 상속(inheritance)  (0) 2023.11.13