JAVA
-
싱글톤 (Singleton) 패턴JAVA/Design Pattern 2022. 1. 1. 15:53
인스턴스를 오직 한 개만 제공하는 클래스 시스템 런타임, 환경 세팅에 대한 정보 등, 인스턴스가 여러 개 일 때 문제가 생길 수 있는 경우가 있다. 인스턴스를 오직 한개만 만들어 제공하는 클래스가 필요하다 방법 1. private 생성자에 static 메소드 public class Singleton { private static Singleton instance; private Singleton() { } public static Singleton getInstance() { if(instance == null){ instance = new Singleton(); } return instance; } } 생성자를 private으로 만든 이유? new키워드를 사용하여 Singleton 클래스의 인스턴스 생..