Item 3. Ensure Singleton with Private Constructor or Enum Type
private 생성자나 열거 타입으로 싱글턴임을 보증하라
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public void leaveTheBuilding() { ... }
}public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private static boolean instanceCreated = false;
private Elvis() {
if (instanceCreated) {
throw new IllegalStateException("Already created.");
}
instanceCreated = true;
}
public void leaveTheBuilding() { ... }
}PreviousItem 2. Consider a Builder When Faced with Many Constructor ParametersNextItem 4. Enforce Noninstantiability with a Private Constructor
Last updated