2023. 1. 12. 09:42γJAVA/Effective JAVA
item05. μμμ μ§μ λͺ μνμ§ λ§κ³ μμ‘΄ κ°μ²΄ μ£Όμ μ μ¬μ©νλΌ.
ν΄λμ€κ° λ΄λΆμ μΌλ‘ νλ μ΄μμ μμμ μμ‘΄νκ³ , κ·Έ μμμ΄ ν΄λμ€μ λμμ μν₯μ μ€λ€λ©΄ μ μ μ νΈλ¦¬ν° ν΄λμ€λ μ±κΈν€ λ°©μμ΄ μ ν©νμ§ μλ€.
λ§μ ν΄λμ€κ° νλ μ΄μμ μμμ μμ‘΄νλλ° μ± μμ μμ λ‘λ λ§μΆ€λ² κ²μ¬κΈ°λ₯Ό μλ‘ λ€κ³ μλ€.
λ§μΆ€λ² κ²μ¬κΈ°λ Dictionaryμ μμ‘΄νλ€κ³ κ°μ νλ€.
1) μ μ μ νΈλ¦¬ν° ν΄λμ€
public class SpellChecker
{
// μμμ μ§μ λͺ
μν μ½λ (μμμ μ§μ μμ±νλ€, new λ‘ μμ± )
private static final Dictionary dictionary = new Dictionary();
private SpellChecker()
{
}
public static boolean isValid(String word)
{
return dictionary.contains(word);
}
public static List<String> suggestions(String typo)
{
return dictionary.closeWordsTo(typo);
}
}
-> μ μ μ νΈλ¦¬ν° ν΄λμ€λ μμμ μ§μ μμ±ν΄μ μ¬μ©νκ³ μλλ° μ΄λ κ² μ¬μ©νλ©΄ μ μ°μ±κ³Ό μ¬μ¬μ©μ±μ΄ λ¨μ΄μ§κ³ , ν μ€νΈνκΈ° μ΄λ ΅λ€.
μ§κΈμ Dictionaryν΄λμ€κ° κ°λ¨νμ§λ§ μλ₯Ό λ€μ΄ μ΄ ν΄λμ€λ₯Ό μ¬μ©νλκ² μμ μλͺ¨κ° λ§λ€κ³ κ°μ νμ.
κ·Έλ¬λ©΄ ν μ€νΈλ₯Ό ν λ μ§κΈμ SpellCheckerμ κΈ°λ₯μ λν΄μλ§ ν μ€νΈ νκ³ μΆμ§λ§ μ΄ κΈ°λ₯μ ν μ€νΈ νκΈ° μν΄μλ Dictionary ν΄λμ€λ₯Ό μμ±ν΄μ μ¬μ©ν΄μΌ νλ€. μ΄λ κ² λλ©΄ ν μ€νΈ νλλ° μ΄λ €μμ§λ€.
κ·Έλ¦¬κ³ μ¬μ¬μ©μ±μ λν΄μ μκ°νλ©΄ μ§κΈμ DictionaryμΈλ° λ§μ½ νκΈ λ§μΆ€λ²μ λν΄μ κ²μ¬νκ³ μΆλ€~ νλ©΄ μ΄λ»κ² ν΄μΌνλκ°..?
νκΈ λ§κ³ μμ΄λ‘ ν λλ? κ·Έλ΄ λ λ§λ€ κ³μ μλ‘μ΄ SpellChekcerλ₯Ό λ§λλ건 κ΅μ₯ν λΉν¨μ¨μ μ΄λ€.
2) μ±κΈν€ λ°©μ
μ μ μ νΈλ¦¬ν° λ°©λ² λ§κ³ λ μ±κΈν€ λ°©μμ΄ μλ€.
public class SpellChecker
{
private final Dictionary dictionary = new Dictionary();
private SpellChecker()
{
}
public static final SpellChecker INSTANCE = new SpellChecker();
public boolean isValid(String word)
{
return dictionary.contains(word);
}
public List<String> suggestions(String typo)
{
return dictionary.closeWordsTo(typo);
}
}
-> μ΄ λ°©μ μμ μμμ μ§μ λͺ μν΄μ£Όκ³ μκΈ° λλ¬Έμ μ¬μ¬μ©μ±, μ μ°μ±μ΄ λ¨μ΄μ§κ³ , ν μ€νΈνκΈ° μ΄λ ΅λ€.
π’ 1) κ³Ό 2) λ°©μ λͺ¨λ μ¬μ μ νλλ§ μ¬μ©ν μ λ°μ μκΈ° λλ¬Έμ μ’μ λ°©λ²μ΄ μλλ€.
μ€μ λ‘λ μ¬μ μ μΈμ΄λ³λ‘ μ‘΄μ¬νκ³ μκ³ νΉμ μ΄νμ© μ¬μ μ΄λ ν μ€νΈ μ¬μ μ΄ νμν κ²½μ°λ μλλ° μ¬μ νλλ‘ μ΄ λͺ¨λ κ²μ λμνκΈ°λ μ΄λ ΅λ€.
μ¦, μ¬μ©νλ μμμ λ°λΌ λμμ΄ λ¬λΌμ§λ ν΄λμ€μλ μ μ μ νΈλ¦¬ν° ν΄λμ€λ μ±κΈν΄ λ°©μμ΄ μ ν©νμ§ μλ€.
3) μμ‘΄ κ°μ²΄ μ£Όμ
1)κ³Ό 2) λ°©λ² λμ μ SpellCheckerκ° μ¬λ¬ μμ μΈμ€ν΄μ€λ₯Ό μ§μν΄μΌ νκ³ , ν΄λΌμ΄μΈνΈκ° μνλ μμ(Dictionary)μ μ¬μ©ν΄μΌ νλ€.
μ΄ λ°©λ²μ΄ λ°λ‘ μμ‘΄ κ°μ²΄ μ£Όμ μΌλ‘ μΈμ€ν΄μ€λ₯Ό μμ±ν λ μμ±μμ νμν μμμ λ겨주λ λ°©μμ΄λ€.
public class SpellChecker
{
private final Dictionary dictionary;
// μΈμ€ν΄μ€λ₯Ό μμ±ν λ νμν μμμ λ겨주λ λ°©μ
public SpellChecker(Dictionary dictionary )
{
this.dictionary = dictionary;
}
public boolean isValid(String word)
{
return dictionary.contains(word);
}
public List<String> suggestions(String typo)
{
return dictionary.closeWordsTo(typo);
}
}
-> μ΄λ κ² μ¬μ©νλ©΄ Dictionary λΌλ λ± νλμ κ°μ²΄λ₯Ό μ¬μ©νμ§λ§ μμμ΄ λͺκ°λ μμ‘΄ κ΄κ³κ° μ΄λ»λ μκ΄μμ΄ μ λμνλ€.
π ν©ν°λ¦¬ λ©μλ ν¨ν΄
μμ λ°©λ²μ λ³νμΌλ‘λ μμ±μμ μμ ν©ν 리λ₯Ό λ겨주λ λ°©μμ΄ μλ€.
ν©ν°λ¦¬λ νΈμΆν λ λ§λ€ νΉμ νμ μ μΈμ€ν΄μ€λ₯Ό λ°λ³΅ν΄μ λ§λ€μ΄μ£Όλ κ°μ²΄λ₯Ό λ§νλ€.
public class SpellChecker
{
private final Dictionary dictionary;
public SpellChecker(DictionaryFactory dictionaryFactory)
{
this.dictionary = dictionaryFactory.get();
}
public boolean isValid(String word)
{
return dictionary.contains(word);
}
public List<String> suggestions(String typo)
{
return dictionary.closeWordsTo(typo);
}
}
public class DictionaryFactory
{
public Dictionary get()
{
return new DefaultDictionary();
}
}
μ΄λ κ² ν©ν°λ¦¬λ₯Ό μ¬μ©ν΄μ νΈμΆν λ λ§λ€ νΉμ μΈμ€ν΄μ€λ₯Ό λ°λ³΅ν΄μ λ§λ€μ΄μ€ μλ μλλ°,
μ΄λ ν¨μν μΈν°νμ΄μ€μΈ Supplierκ° ν©ν°λ¦¬λ₯Ό ννν μλ²½ν μλΌκ³ ν μ μλ€.
[μ΄νν°λΈ μλ°] Item03 μ벽곡λ΅. ν¨μν μΈν°νμ΄μ€ (tistory.com)
Supplier<T>λ μΈμλ₯Ό λ°μ§ μκ³ returnκ°λ§ μ‘΄μ¬νλ ν¨μμ΄λ€. (곡κΈμ μν )
public class SpellChecker
{
private final Dictionary dictionary;
// μΈμ€ν΄μ€λ₯Ό μμ±ν λ νμν μμμ λ겨주λ λ°©μ
public SpellChecker(Supplier<Dictionary> factorySupplier) // Supplier<T>κ° ν©ν°λ¦¬λ₯Ό ννν μλ²½ν μ
{
this.dictionary = factorySupplier.get();
}
public boolean isValid(String word)
{
return dictionary.contains(word);
}
public List<String> suggestions(String typo)
{
return dictionary.closeWordsTo(typo);
}
}
Supplier<T>λ₯Ό μ λ ₯μΌλ‘ λ°λ λ©μλλ μΌλ°μ μΌλ‘ νμ μ μμΌλ μΉ΄λ νμ μ μ¬μ©ν΄ ν©ν°λ¦¬μ νμ 맀κ°λ³μλ₯Ό μ νν΄μΌ νλ€.
public class SpellChecker
{
private final Dictionary dictionary;
// μΈμ€ν΄μ€λ₯Ό μμ±ν λ νμν μμμ λ겨주λ λ°©μ
public SpellChecker(Supplier<? extends Dictionary> factorySupplier) // Supplier<T>κ° ν©ν°λ¦¬λ₯Ό ννν μλ²½ν μ
{
this.dictionary = factorySupplier.get();
}
public boolean isValid(String word)
{
return dictionary.contains(word);
}
public List<String> suggestions(String typo)
{
return dictionary.closeWordsTo(typo);
}
}
-> < ? extends Dictionary> λ₯Ό ν΅ν΄μ ν΄λΌμ΄μΈνΈλ μμ μ΄ λͺ μν νμ μ νμ νμ μ΄λΌλ©΄ 무μμ΄λ μμ±ν μ μλ ν©ν°λ¦¬λ₯Ό λκΈΈ μμλ€.
π©π»
μ¬μ©νλ μμμ λ°λΌ λμμ΄ λ¬λΌμ§λ ν΄λμ€λ μ μ μ νΈλ¦¬ν° ν΄λμ€λ μ±κΈν€ λ°©μμ μ¬μ©νλ κ²μ μ’μ§ μμ λ°©λ²μ΄λ€.
μ΄ λμ νμν μμμ (νΉμ μ΄ μμμ μμ±ν΄μ£Όλ ν©ν°λ¦¬λ₯Ό) μμ±μμ λ겨주λ μμ‘΄ κ°μ²΄ μ£Όμ μ μ¬μ©νλ κ²μ κΆμ₯νλ€.
μμ‘΄ κ°μ²΄ μ£Όμ μ μ¬μ©νλ©΄ ν΄λμ€μ μ μ°μ±, μ¬μ¬μ©μ±, ν μ€νΈ μ©μ΄μ±μ κ°μ ν μ μλ€.
κ·Όλ° μμ‘΄μ±μ΄ λ§μ½ μμ²κ°κ° λλ ν° νλ‘μ νΈλ€ νλ©΄ μ΄ μμ‘΄ κ°μ²΄ μ£Όμ μ΄ μ½λλ₯Ό μ§μ λΆνκ² λ§λ€κΈ°λ νλ€.
(μμ‘΄μ±μ΄ μμ²κ°λ§ μμ‘΄ μ£Όμ μ½λλ§ μμ²μ€ λ λ―..)
-> μ΄μ λν΄μλ λκ±° Dagger, μ£Όμ€ Guice, μ€νλ§ Spring κ³Ό κ°μ μμ‘΄ κ°μ²΄ μ£Όμ νλ μμν¬λ₯Ό μ¬μ©νλ©΄ ν΄μν μ μλ€.