#10 effective java: item 5
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.example.effectivejava.chapter01.item05;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MockDictionary implements Dictionary{
|
||||
@Override
|
||||
public boolean contains(String word) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> closeWordsTo(String typo) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.effectivejava.chapter01.item05.dependencyinjection;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.DefaultDictionary;
|
||||
|
||||
public class DictionaryFactory {
|
||||
public static DefaultDictionary get() {
|
||||
return new DefaultDictionary();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.example.effectivejava.chapter01.item05.dependencyinjection;
|
||||
import com.example.effectivejava.chapter01.item05.Dictionary;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class SpellChecker {
|
||||
|
||||
@@ -12,6 +13,10 @@ public class SpellChecker {
|
||||
this.dictionary = dictionary;
|
||||
}
|
||||
|
||||
public SpellChecker(Supplier<Dictionary> dictionarySupplier) {
|
||||
this.dictionary = dictionarySupplier.get();
|
||||
}
|
||||
|
||||
public boolean isValid(String word) {
|
||||
// TODO 여기 SpellChecker 코드
|
||||
return dictionary.contains(word);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.effectivejava.chapter01.item05.factorymethod;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.DefaultDictionary;
|
||||
import com.example.effectivejava.chapter01.item05.Dictionary;
|
||||
|
||||
public class DefaultDictionaryFactory implements DictionaryFactory {
|
||||
@Override
|
||||
public Dictionary getDictionary() {
|
||||
return new DefaultDictionary();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.effectivejava.chapter01.item05.factorymethod;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.Dictionary;
|
||||
|
||||
public interface DictionaryFactory {
|
||||
|
||||
Dictionary getDictionary();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.effectivejava.chapter01.item05.factorymethod;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.Dictionary;
|
||||
import com.example.effectivejava.chapter01.item05.MockDictionary;
|
||||
|
||||
public class MockDictionaryFactory implements DictionaryFactory {
|
||||
@Override
|
||||
public Dictionary getDictionary() {
|
||||
return new MockDictionary();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.effectivejava.chapter01.item05.factorymethod;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.Dictionary;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.effectivejava.chapter01.item05.springioc;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
|
||||
SpellChecker spellChecker = applicationContext.getBean(SpellChecker.class);
|
||||
spellChecker.isValid("test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.effectivejava.chapter01.item05.springioc;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.Dictionary;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackageClasses = AppConfig.class)
|
||||
public class AppConfig {
|
||||
|
||||
// @Bean
|
||||
// public SpellChecker spellChecker(Dictionary dictionary) {
|
||||
// return new SpellChecker(dictionary);
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public Dictionary dictionary() {
|
||||
// return new SpringDictionary();
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.effectivejava.chapter01.item05.springioc;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.Dictionary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class SpellChecker {
|
||||
|
||||
private 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,20 @@
|
||||
package com.example.effectivejava.chapter01.item05.springioc;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.Dictionary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class SpringDictionary implements Dictionary {
|
||||
@Override
|
||||
public boolean contains(String word) {
|
||||
System.out.println("word = " + word);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> closeWordsTo(String typo) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
package com.example.effectivejava.chapter01.item05.dependencyinjection;
|
||||
|
||||
import com.example.effectivejava.chapter01.item05.DefaultDictionary;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SpellCheckerTest {
|
||||
class SpellCheckerTest {
|
||||
|
||||
@Test
|
||||
void isValid() {
|
||||
SpellChecker spellChecker = new SpellChecker(new DefaultDictionary());
|
||||
SpellChecker spellChecker = new SpellChecker(DictionaryFactory::get);
|
||||
spellChecker.isValid("test");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user