사용하는 자원에 따라 동작이 달라지는 클래스는 정적 유틸리티 클래스나 싱글턴방식이 적합하지 않다.
의존 객체 주입이란 인스턴스를 생성할 때 필요한 자원을 넘겨주는 방식이다.
이 방식의 변형으로 생성자에 자원 팩터리를 넘겨줄 수 있다.
의존 객체 주입을 사용하면 클래스의 유연성, 재사용성, 테스트 용이성을 개선할 수있다
public class SpellChecker {
private static final Dictionary dictionary = new DefaultDictionary();
private SpellChecker() {}
public static boolean isValid(String word) {
// TODO 여기 SpellChecker 코드
return dictionary.contains(word);
}
public static List<String> suggestions(String typo) {
// TODO 여기 SpellChecker 코드
return dictionary.closeWordsTo(typo);
}
}
===
public interface Dictionary {
boolean contains(String word);
List<String> closeWordsTo(String typo);
}
===
public class DefaultDictionary implements Dictionary{
@Override
public boolean contains(String word) {
return false;
}
@Override
public List<String> closeWordsTo(String typo) {
return null;
}
}
===
class SpellCheckerTest {
@Test
void isValid() {
SpellChecker spellChecker = new SpellChecker(MockDictionary::new);
spellChecker.isValid("test");
}
}
p29, 이 패턴의 쓸만한 변형으로 생성자에 자원 팩터리를 넘겨주는 방식이 있다.
p29, 자바 8에서 소개한 Supplier<T> 인터페이스가 팩터리를 표현한 완벽한 예 다.
public class SpellChecker {
private final Dictionary dictionary;
public SpellChecker(Dictionary dictionary) {
this.dictionary = dictionary;
}
public SpellChecker(Supplier<Dictionary> dictionarySupplier) {
this.dictionary = dictionarySupplier.get();
}
===
public class DictionaryFactory {
public static DefaultDictionary get() {
return new DefaultDictionary();
}
}
p29, 한정적 와일드카드 타입을 사용해 팩터리의 타입 매개변수를 제한해야 한다.
public SpellChecker(Supplier<? extends Dictionary> dictionarySupplier) {
this.dictionary = dictionarySupplier.get();
}
~~public SpellChecker(Supplier<DictionaryFactory > dictionarySupplier) {
this.dictionary = dictionarySupplier.get();
} -> Mock.test 불가~~
p29, 팩터리 메소드 패턴
// createInterface
public interface DictionaryFactory {
Dictionary getDictionary();
}
===
public class DefaultDictionaryFactory implements DictionaryFactory {
@Override
public Dictionary getDictionary() {
return new DefaultDictionary();
}
}
===
public class MockDictionaryFactory implements DictionaryFactory {
@Override
public Dictionary getDictionary() {
return new MockDictionary();
}
}
===
public class SpellChecker {
private Dictionary dictionary;
public SpellChecker(DictionaryFactory dictionaryFactory) {
this.dictionary = dictionaryFactory.getDictionary();
}
public boolean isValid(String word) {
// TODO 여기 SpellChecker 코드
return dictionary.contains(word);
}
public List<String> suggestions(String typo) {
// TODO 여기 SpellChecker 코드
return dictionary.closeWordsTo(typo);
}
}