πŸ“‚ JAVA/μ΄νŽ™ν‹°λΈŒ μžλ°”

μžμ›μ„ 직접 λͺ…μ‹œν•˜μ§€ 말고 의쑴 객체 μ£Όμž…μ„ μ‚¬μš©ν•˜λΌ - [2μž₯ 객체 생성과 파괴(μ•„μ΄ν…œ5)]

Amenable 2022. 11. 28. 15:56

  μ•„μ΄ν…œμ˜ 제λͺ©μ„ 톡해 'λͺ¨λ“  κ²½μš°μ— 의쑴 객체 μ£Όμž…μ„ μ‚¬μš©ν•΄μ•Όκ² λ‹€'라고 μ΄ν•΄ν•˜μ§€ μ•ŠκΈ°λ₯Ό λ°”λž€λ‹€. μ‚¬μš©ν•˜λŠ” μžμ›μ— 따라 λ™μž‘μ΄ λ‹¬λΌμ§€λŠ” 클래슀인 κ²½μš°μ— '의쑴 객체 μ£Όμž…'을 고렀해보도둝 ν•˜μž. (μ•žμœΌλ‘œ λ‚˜μ˜¬ μ˜ˆμ‹œμ—μ„œλ„ 확인할 수 μžˆλ“―μ΄ μ‚¬μ „μ˜ μ’…λ₯˜(Dictionary)에 따라 SpellCheckerλΌλŠ” 클래슀의 λ™μž‘μ΄ λ‹¬λΌμ§€λŠ” κ²½μš°μ— 의쑴 객체 μ£Όμž…μ„ μ‚¬μš©ν•˜λŠ” 것을 μ•Œ 수 μžˆλ‹€.)

 

public class SpellChecker{
    private static final Dictionary dictionary = new Dictionary(); // 이것이 제λͺ©μ—μ„œ λ‚˜μ˜¨ μžμ›μ„ 직접 λͺ…μ‹œν•˜λŠ” μ˜ˆμ‹œμ΄λ‹€.
    
    private SpellChecker(){}
    
    public static boolean isValid(String word){
        // 여기에 SpellChecker μ½”λ“œκ°€ μΆ”κ°€μ μœΌλ‘œ μžˆλ‹€κ³  κ°€μ •ν•˜μž
        return dictionary.contains(word);
    }
    
    public static List<String> suggestions(String typo){
        // 여기에 SpellChecker μ½”λ“œκ°€ μΆ”κ°€μ μœΌλ‘œ μžˆλ‹€κ³  κ°€μ •ν•˜μž
        return dictionary.closeWordsTo(typo);
    }
}

  ν•΄λ‹Ή μ½”λ“œμ˜ 첫 번째 λ¬Έμ œμ μ€ ν…ŒμŠ€νŠΈμ˜ 어렀움이닀. μš°μ„ , λ©”μ„œλ“œ μ•ˆμ— μžˆλŠ” μ½”λ“œλ“€μ„ κ°€μ§œ dictionary둜 ν…ŒμŠ€νŠΈλ₯Ό ν•  수 μ—†λŠ” λ¬Έμ œκ°€ μžˆλ‹€. 그리고 dictionaryλ₯Ό λ§Œλ“œλŠ”λ° λ§Žμ€ λΉ„μš©μ΄ 든닀라고 κ°€μ •ν•˜λ©΄(CPU와 λ©”λͺ¨λ¦¬λ₯Ό 많이 μ“΄λ‹€κ³  κ°€μ •), ν…ŒμŠ€νŠΈλ₯Ό λ§Œλ“œλŠ”λ° λ§Žμ€ λΉ„νš¨μœ¨μ΄ λ°œμƒν•œλ‹€. μœ„μ˜ μ˜ˆμ‹œμ˜ ν…ŒμŠ€νŠΈ μ½”λ“œλŠ” μ•„λž˜μ²˜λŸΌλ§Œ λ§Œλ“€ 수 μžˆμ„ κ±°λ‹€.

class SpellCheckerTest{
    @Test
    void isValid(){
        assertTrue(SpellChecker.isValid("test"));
    }
}

 

  두 번째 λ¬Έμ œμ μ€ μœ μ—°μ„±κ³Ό μž¬μ‚¬μš©μ„±μ΄ λ–¨μ–΄μ§„λ‹€λŠ” 것이닀. ν˜„μž¬ 'ν•œκ΅­μ–΄ 사전'만 지원해주고 μžˆλŠ”λ° 'μ˜μ–΄ 사전', '일본어 사전', '쀑ꡭ어 사전'등을 지원해쀀닀고 μƒκ°ν•˜λ©΄ κ·Έ 이유λ₯Ό μ•Œ 수 μžˆμ„ 것이닀.

 

  두 κ°€μ§€ λ¬Έμ œμ μ— λŒ€ν•œ ν•΄κ²°μ±…μœΌλ‘œ μ±…μ—μ„œλŠ” 의쑴 객체 μ£Όμž…μ„ μ„€λͺ…ν•˜κ³  μžˆλ‹€.

public class SpellChecker{
    private final Dictionary dictionary;
    
    public SpellChecker(Dictionary dictionary){
        this.dictionary = dictionary;
    }
    
    public static boolean isValid(String word){
        // 여기에 SpellChecker μ½”λ“œκ°€ μΆ”κ°€μ μœΌλ‘œ μžˆλ‹€κ³  κ°€μ •ν•˜μž
        return dictionary.contains(word);
    }
    
    public static List<String> suggestions(String typo){
        // 여기에 SpellChecker μ½”λ“œκ°€ μΆ”κ°€μ μœΌλ‘œ μžˆλ‹€κ³  κ°€μ •ν•˜μž
        return dictionary.closeWordsTo(typo);
    }
}

  μ΄λ•Œ, DictionaryλŠ” μΈν„°νŽ˜μ΄μŠ€μ—¬μ•Ό ν•œλ‹€.

public interface Dictionary{
    boolean contains(String word);
    
    List<String> closeWordsTo(String typo);
}

  μ½”λ“œμ—μ„œ 확인할 수 μžˆλ“―μ΄ μ΄λŠ” μœ μ—°μ„±κ³Ό μž¬μ‚¬μš©μ„±μ„ 확보할 수 μžˆλ‹€.(μ˜μ–΄ 사전, 일본어 사전, 쀑ꡭ어 사전이 μ‰½κ²Œ 적용될 것이닀.)

  그리고 κ°€μ§œ dictionaryλ₯Ό μ•„λž˜μ™€ 같이 μ μš©ν•  수 있기 λ•Œλ¬Έμ— ν…ŒμŠ€νŠΈμ˜ λ¬Έμ œμ λ„ ν•΄κ²°λœλ‹€.

// DefaultDictionaryλ₯Ό ν…ŒμŠ€νŠΈμš© dictionary라고 κ°€μ •ν•˜μž
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(new DefaultDictionary());
        assertTrue(SpellChecker.isValid("test"));
    }
}

 

[정리]

  ν΄λž˜μŠ€κ°€ λ‚΄λΆ€μ μœΌλ‘œ ν•˜λ‚˜ μ΄μƒμ˜ μžμ›μ— μ˜μ‘΄ν•˜κ³ , κ·Έ μžμ›μ΄ 클래슀 λ™μž‘μ— 영ν–₯을 μ€€λ‹€λ©΄ μ˜μ‘΄ 객체 μ£Όμž…μ„ μ‚¬μš©ν•΄λ³΄μž. μ΄λŠ” 클래슀의 μœ μ—°μ„±, μž¬μ‚¬μš©μ„± 그리고 ν…ŒμŠ€νŠΈ μš©μ΄μ„±μ„ κΈ°λ§‰νžˆκ²Œ κ°œμ„ ν•΄μ€„ 것이닀. 

 

ν•΄λ‹Ή 글은 λ°±κΈ°μ„  λ‹˜μ˜ 'μ΄νŽ™ν‹°λΈŒ μžλ°” μ™„λ²½ 곡랡'을 μˆ˜κ°•ν•˜κ³  μž‘μ„±ν•œ κ²ƒμž…λ‹ˆλ‹€.