사용하는 자원에 따라 동작이 달라지는 클래스에는 정적 유틸리티 클래스나 싱글턴 방식이 적합하지 않다.
의존 객체 주입은 유연성과 테스트 용이성을 높여준다. 인스턴스를 생성할 때 생성자에 필요한 자원을 넘겨주는 방식이다.
public class SpellChecker {
private final Lexicon dictionary;
public SpellChecker (Lexicon dictionary){
this.dictionary = Objects.requireNonnull(dictionary);
}
public boolean isValid(String word) {...}
public List<String> suggestions(String type) {...}
}