#10 effective java: item 5
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user