Dev-/Java

싱글톤 객체 성능 향상 --> LazyHolder 사용

thiago6 2019. 2. 28. 15:32
public class Singleton {
private static Singleton test = new Singleton();

public static Singleton getInstance() {
return test;
}

private Singleton() { }
}

--> 필요없는 메모리 낭비 발생



public class Singleton {
private static class LazyHolder {
public static final Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance() {
return LazyHolder.INSTANCE;
}

private Singleton() { }
}

--> 최초 1번 호출될 때 생성 --> 메모리 낭비 방지