pg. 29 의존 객체 주입은 유연성과 테스트 용이성을 높여준다.
public class SpellChecker3 {
private final Lexicon dictionary;
public SpellChecker3(Lexicon dictionary) {
this.dictionary = Objects.requireNonNull(dictionary);
}
public boolean isValid(String word) {
throw new UnsupportedOperationException();
}
public List<String> suggestions(String typo) {
throw new UnsupportedOperationException();
}
public static void main(String[] args) {
Lexicon lexicon = new KoreanDictionary();
//Lexicon lexicon = new TestDictionary();
SpellChecker3 spellChecker = new SpellChecker3(lexicon);
}
}
의존객체 주입을 통해 OCP를 지킬 수 있다.
[객체지향] OCP(Open-Close Principle)
- 변경에는 닫혀(Close)있어야 하고 확장에는 열려(Open)있어야 한다.
- 즉, 기존의 코드를 변경하지 않으면서 기능을 추가할 수 있도록 설계가 되어야 한다.
- 객체지향설계 5대 원칙 SOLID 중 하나
의존객체 주입을 통해 OCP를 지킬 수 있다.
[객체지향] OCP(Open-Close Principle)