멤버 자원이 클래스 동작에 영향을 준다면 싱글턴과 정적 유틸리티 클래스를 사용하지말고, 의존 객체 주입을 하도록 하자.
class SpellChecker {
private final Lexicon dictionary;
SpellChecker(Lexicon dictionary) {
this.dictionary = Objects.requireNonNull(dictionary);
}
boolean isValid(String word) { ... }
}
class SpellChecker {
private static final Lexicon dictionary = ...;
private SpellChecker() {}
boolean static isValid(String word) { ... }
}
class SpellChecker {
private final Lexicon dictionary = ...;
public static final SpellChecker INSTANCE = new SpellChecker();
private SpellChecker() {}
boolean static isValid(String word) { ... }
}