#10 effective java: item 5

This commit is contained in:
haerong22
2022-05-18 01:57:22 +09:00
parent 57fcca02d9
commit c6cf9334e2
7 changed files with 127 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package com.example.effectivejava.chapter01.item05;
import java.util.List;
public class DefaultDictionary implements Dictionary{
@Override
public boolean contains(String word) {
return false;
}
@Override
public List<String> closeWordsTo(String typo) {
return null;
}
}

View File

@@ -0,0 +1,10 @@
package com.example.effectivejava.chapter01.item05;
import java.util.List;
public interface Dictionary {
boolean contains(String word);
List<String> closeWordsTo(String typo);
}

View File

@@ -0,0 +1,24 @@
package com.example.effectivejava.chapter01.item05.dependencyinjection;
import com.example.effectivejava.chapter01.item05.Dictionary;
import java.util.List;
public class SpellChecker {
private final Dictionary dictionary;
public SpellChecker(Dictionary dictionary) {
this.dictionary = dictionary;
}
public boolean isValid(String word) {
// TODO 여기 SpellChecker 코드
return dictionary.contains(word);
}
public List<String> suggestions(String typo) {
// TODO 여기 SpellChecker 코드
return dictionary.closeWordsTo(typo);
}
}

View File

@@ -0,0 +1,25 @@
package com.example.effectivejava.chapter01.item05.singleton;
import com.example.effectivejava.chapter01.item05.DefaultDictionary;
import com.example.effectivejava.chapter01.item05.Dictionary;
import java.util.List;
public class SpellChecker {
private final Dictionary dictionary = new DefaultDictionary();
private SpellChecker() {}
public static final SpellChecker INSTANCE = new SpellChecker();
public boolean isValid(String word) {
// TODO 여기 SpellChecker 코드
return dictionary.contains(word);
}
public List<String> suggestions(String typo) {
// TODO 여기 SpellChecker 코드
return dictionary.closeWordsTo(typo);
}
}

View File

@@ -0,0 +1,24 @@
package com.example.effectivejava.chapter01.item05.staticutils;
import com.example.effectivejava.chapter01.item05.DefaultDictionary;
import com.example.effectivejava.chapter01.item05.Dictionary;
import java.util.List;
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);
}
}

View File

@@ -0,0 +1,14 @@
package com.example.effectivejava.chapter01.item05.dependencyinjection;
import com.example.effectivejava.chapter01.item05.DefaultDictionary;
import org.junit.jupiter.api.Test;
class SpellCheckerTest {
@Test
void isValid() {
SpellChecker spellChecker = new SpellChecker(new DefaultDictionary());
spellChecker.isValid("test");
}
}

View File

@@ -0,0 +1,14 @@
package com.example.effectivejava.chapter01.item05.staticutils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SpellCheckerTest {
@Test
void isValid() {
assertTrue(SpellChecker.isValid("test"));
}
}