#10 effective java: item 5

This commit is contained in:
haerong22
2022-05-19 01:03:43 +09:00
parent c6cf9334e2
commit 69ddddd1ce
12 changed files with 165 additions and 3 deletions

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -0,0 +1,8 @@
package com.example.effectivejava.chapter01.item05.factorymethod;
import com.example.effectivejava.chapter01.item05.Dictionary;
public interface DictionaryFactory {
Dictionary getDictionary();
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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");
}
}

View File

@@ -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();
// }
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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");
}