-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from SSONG22/item5-songi
[#5][2κΈ°] μμμ μ§μ λͺ μνμ§ λ§κ³ μμ‘΄ κ°μ²΄ μ£Όμ μ μ¬μ©νλΌ
- Loading branch information
Showing
1 changed file
with
262 additions
and
0 deletions.
There are no files selected for viewing
262 changes: 262 additions & 0 deletions
262
2μ₯/5_μμμ_μ§μ _λͺ
μνμ§_λ§κ³ _μμ‘΄ κ°μ²΄_μ£Όμ
μ_μ¬μ©νλΌ_μμ‘μ΄.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
# μμ΄ν 5 μμμ μ§μ λͺ μνμ§ λ§κ³ μμ‘΄ κ°μ²΄ μ£Όμ μ μ¬μ©νλΌ | ||
|
||
--- | ||
|
||
> ν΄λμ€κ° λ΄λΆμ μΌλ‘ νλ μ΄μμ **μμ**μ μμ‘΄νκ³ , | ||
κ·Έ μμμ΄ ν΄λμ€ λμμ μν₯μ μ€λ€λ©΄ | ||
μ±κΈν΄(Singleton) κ³Ό, μ μ μ νΈλ¦¬ν°(static util class)λ μ¬μ©νμ§ μλ κ²μ΄ μ’λ€. | ||
|
||
*μ¦, μ¬μ©νλ μμμ λ°λΌ λμμ΄ λ¬λΌμ§λ ν΄λμ€μλ μ μ μ νΈλ¦¬ν° ν΄λμ€λ μ±κΈν΄ λ°©μμ΄ μ ν©νμ§ μλ€.* | ||
|
||
- μ μ μ νΈλ¦¬ν°λ₯Ό μλͺ» μ¬μ©ν μ - μ μ°νμ§ μκ³ ν μ€νΈνκΈ° μ΄λ ΅λ€ | ||
|
||
```java | ||
// μ μ μ νΈλ¦¬ν°λ₯Ό μλͺ» μ¬μ©ν μ - μ μ°νμ§ μκ³ ν μ€νΈνκΈ° μ΄λ ΅λ€ | ||
public class SpellChecker{ | ||
private static final Lexicon dictionary = ...; // μ¬μ μ μμ‘΄ | ||
private SpellChecker() {} // κ°μ²΄ μμ± λ°©μ§ | ||
public static boolean isValid(String word){...} | ||
public static List<String> suggestions(String typo){...} | ||
} | ||
``` | ||
|
||
- μ±κΈν΄μ μλͺ» μ¬μ©ν μ - μ μ°νμ§ μκ³ ν μ€νΈνκΈ° μ΄λ ΅λ€ | ||
|
||
```java | ||
// μ±κΈν΄μ μλͺ» μ¬μ©ν μ - μ μ°νμ§ μκ³ ν μ€νΈνκΈ° μ΄λ ΅λ€ | ||
public class SpellChecker{ | ||
private static final Lexicon dictionary = ...; // μ¬μ μ μμ‘΄ | ||
private SpellChecker(...) {} | ||
public static SpellChecker INSTANCE = new SpellChecker(...); | ||
|
||
public static boolean isValid(String word){...} | ||
public static List<String> suggestions(String typo){...} | ||
|
||
} | ||
``` | ||
|
||
- SpellChecker ν΄λμ€λ dictionary(μμ)μ μμ‘΄νκ³ μλ€ | ||
- κ·Έλ°λ°, μΈμ΄λ³ μ¬μ , νΉμ μ΄νμ© μ¬μ μ΄ νμνκ² λλ©΄ ν΄λμ€ λ΄λΆ λ©μλ λμμ΄ λ¬λΌμ ΈμΌ νλ€. | ||
- μ μ μ νΈλ¦¬ν° ν΄λμ€λ μ±κΈν΄ λ°©μμ μ ν©νμ§ μλ€. | ||
|
||
> μλνλ©΄, μ μ°νμ§ μκ³ , ν μ€νΈ νκΈ° μ΄λ ΅κΈ° λλ¬Έμ΄λ€. | ||
|
||
- SpellCheckerκ° μ¬λ¬ μ¬μ μ μ¬μ©ν μ μλλ‘ λ§λ€κΈ° μν΄ dictionary νλμμ final νμ μλ₯Ό μ κ±°νκ³ λ€λ₯Έ μ¬μ μΌλ‘ κ΅μ²΄νλ λ©μλλ₯Ό μΆκ°ν μλ μλ€. | ||
- κ·Έλ¬λ μ΄ λ°©λ²μ μ΄μνλ©°, μ€λ₯λ₯Ό λ΄κΈ° μ½λ€. λν λ©ν°μ€λ λ νκ²½μμλ μΈ μ μλ€. | ||
|
||
> ν΄λμ€κ° μ¬λ¬ μμ μΈμ€ν΄μ€λ₯Ό μ§μν΄μΌνκ³ , ν΄λΌμ΄μΈνΈκ° μνλ μμμ μ¬μ©νκ² νλ λ°©λ²μ μ¬μ©ν΄μΌ νλ€. | ||
μΈμ€ν΄μ€λ₯Ό μμ±ν λ μμ±μμ νμν μμμ λ겨주μ. | ||
|
||
- **μμ‘΄ κ°μ²΄ μ£Όμ λ°©μ** μ΄λΌκ³ νλ€. | ||
|
||
```java | ||
// μμ‘΄ κ°μ²΄ μ£Όμ μ μ μ°μ±κ³Ό ν μ€νΈ μ©μ΄μ±μ λμ¬μ€λ€ | ||
public class SpellChecker{ | ||
private final Lexicon dictionary; | ||
public SpellChecker(Lexicon dictionary){ | ||
this.dictionay = Objects.requiredNonNull(dictionay); | ||
} | ||
public boolean isValid(String word){...} | ||
public List<String> suggestions(String typo){...} | ||
} | ||
``` | ||
|
||
- ν΄λμ€μ μ μ°μ±, μ¬μ¬μ©μ±, ν μ€νΈ μ©μ΄μ±μ κ°μ ν΄μ€λ€. | ||
- λΆλ³μ 보μ₯ν΄ ν΄λΉ μμμ μ¬μ©νλ €λ μ¬λ¬ ν΄λΌμ΄μΈνΈκ° μμ‘΄ κ°μ²΄λ€μ μμ¬νκ³ κ³΅μ ν μ μλ€. | ||
- μμ±μ, μ μ ν©ν°λ¦¬, λΉλ μμ΄ν μ μμ©ν μ μλ€. | ||
|
||
```java | ||
public class λ°°λ¬μλΉμ€{ | ||
private final λ°°λ¬ μΉν¨λ°°λ¬; | ||
// μ μ μ νΈλ¦¬ν° | ||
private λ°°λ¬μλΉμ€(){}; //κ°μ²΄ μμ± λ°©μ§ | ||
public static void λ°°λ¬μμ(){} | ||
} | ||
public class λ°°λ¬μλΉμ€{ | ||
private final λ°°λ¬ μΉν¨λ°°λ¬; | ||
private λ°°λ¬μλΉμ€(..){..}; | ||
public static λ°°λ¬μλΉμ€ Instance = new λ°°λ¬μμμλΉμ€(..); // μ±κΈν΄ ν¨ν΄ | ||
public static void λ°°λ¬μμ(){} | ||
} | ||
// μΉν¨λ°°λ¬λ§ κ°λ₯νλ€ ... | ||
``` | ||
|
||
```java | ||
public class λ°°λ¬μλΉμ€{ | ||
private final λ°°λ¬ μμλ°°λ¬; | ||
public λ°°λ¬μλΉμ€(λ°°λ¬ μμμ΄λ¦){ | ||
this.μμλ°°λ¬ = μμμ΄λ¦; | ||
} | ||
public static void λ°°λ¬μμ(){} | ||
} | ||
// μ΄λ€ μμλ λ°°λ¬ν μ μλ μλΉμ€κ° λμλ€ ! | ||
``` | ||
|
||
> μμ‘΄ κ°μ²΄ μ£Όμ μμ© : μμ±μμ μμ ν©ν°λ¦¬(Factory) λ₯Ό λ겨 μ£Όλ λ°©μ | ||
- ν©ν°λ¦¬(Factory) : νΈμΆν λλ§λ€ νΉμ νμ μ μΈμ€ν΄μ€λ₯Ό λ°λ³΅ν΄μ λ§λ€μ΄μ£Όλ κ°μ²΄ | ||
- ν©ν°λ¦¬ λ©μλ ν¨ν΄ (Factory Method pattern) : μμ±μμ μμ ν©ν°λ¦¬λ₯Ό λ겨주λ λ°©μ | ||
|
||
<details> | ||
<summary>ν©ν°λ¦¬ λ©μλ ν¨ν΄</summary> | ||
<div markdown="1"> | ||
|
||
- ν©ν°λ¦¬ λ©μλ ν¨ν΄ | ||
- κ°μ²΄ μμ±μ μΊ‘μν νλ ν¨ν΄μ΄λ€ | ||
- κ°μ²΄λ₯Ό μμ±νκΈ° μν μΈν°νμ΄μ€λ₯Ό μ μνμ§λ§, μ΄λ€ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό μμ±ν μ§μ λν κ²°μ μ μλΈ ν΄λμ€κ° λ΄λ¦°λ€. | ||
- Creatorμ μλΈ ν΄λμ€μ ν©ν 리 λ©μλλ₯Ό μ μ νλ©°, ν©ν°λ¦¬ λ©μλ νΈμΆλ‘ ConcreteProduct μΈμ€ν΄μ€λ₯Ό λ°ννκ² νλ€. | ||
- μμ μ½λ | ||
|
||
```java | ||
// Product μν | ||
public interface Shopping { | ||
// μΌνμ ν λ κ³΅ν΅ κ³Όμ | ||
void find(); | ||
void cart(); | ||
void order(); | ||
} | ||
|
||
// Creator (Factory) | ||
public abstract class Shop { | ||
public Shopping shopping(String category){ | ||
Shopping shopping = selectCategory(category); | ||
shopping.find(); | ||
shopping.cart(); | ||
shopping.order(); | ||
return shopping; | ||
} | ||
|
||
// factory method | ||
abstract Shopping selectCategory(String category); | ||
} | ||
``` | ||
|
||
```java | ||
public class MarketOne extends Shop { | ||
@Override | ||
Shopping selectCategory(String category) { | ||
System.out.println("THIS IS MARKET ONE."); | ||
if (category.equals("FOOD")) return new MarketOneFoodCategory(); | ||
if (category.equals("HEALTH")) return new MarketOneHealthCategory(); | ||
return null; | ||
} | ||
} | ||
public class MarketOneFoodCategory implements Shopping { | ||
@Override | ||
public void find() { | ||
System.out.println("market-one: food κ²μ "); | ||
} | ||
|
||
@Override | ||
public void cart() { | ||
System.out.println("market-one: food λ΄κΈ° "); | ||
} | ||
|
||
@Override | ||
public void order() { | ||
System.out.println("market-one: food μ£Όλ¬Έ "); | ||
} | ||
} | ||
``` | ||
</div> | ||
</details> | ||
|
||
- μλ° 8μ Supplier<T> μΈν°νμ΄μ€κ° 리μμ€μ μμ±μμ μ λ¬νλ ν©ν°λ¦¬λ‘ μ°κΈ°μ μ ν©νλ€. | ||
|
||
```java | ||
Mosaic create(Supplier<? extends Tile> tileFactory){..} | ||
``` | ||
|
||
--- | ||
|
||
- μμ‘΄μ±μ΄ μμ²κ°κ° λλ ν° νλ‘μ νΈμμλ μμ‘΄ κ°μ²΄ μ£Όμ μ΄ μ½λλ₯Ό μ΄μ§λ½κ² λ§λ€κΈ°λ νλ€. | ||
- λκ±°, μ£Όμ€, μ€νλ§ κ°μ μμ‘΄ κ°μ²΄ μ£Όμ νλ μμν¬λ₯Ό μ¬μ©νλ©΄ μ΄λ° μ΄μ§λ¬μ§μ ν΄μν μ μλ€. | ||
|
||
<details> | ||
<summary>μ€νλ§ μμ‘΄μ± μ£Όμ </summary> | ||
<div markdown="1"> | ||
|
||
- μ€νλ§ μμ‘΄μ± μ£Όμ | ||
- μ€νλ§ ν΅μ¬ κΈ°λ₯ DI (Dependency Injection) | ||
|
||
**μΈλΆ**(IOC컨ν μ΄λ) μμ κ°μ²΄λ₯Ό μμ±ν ν μμ‘΄μ±μ μ£Όμ μν¨λ€ | ||
|
||
```java | ||
@Service | ||
public class ShopService{ | ||
public void findAllShop(){ | ||
ShopRepository shopRepository = new FoodShop(); | ||
shopRepository.findAll(); | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
@Service | ||
public class ShopService{ | ||
private ShopRepository shopRepository; | ||
public ShopService(ShopRepository shopRepository){ | ||
this.shopRepository = shopRepository; | ||
} | ||
public void findAllShop(){ | ||
shopRepository.findAll(); | ||
} | ||
} | ||
|
||
@Test | ||
public class testFindAll(){ | ||
ShopRepository foodShop = new FoodShop(); | ||
ShopService shopService = new ShopService(foodShop); | ||
shopService.findAll(); | ||
} | ||
``` | ||
|
||
μμ‘΄μ± μ£Όμ λ°©λ² | ||
|
||
1. μμ±μ μ΄μ© | ||
|
||
```java | ||
@Service | ||
public class ShopService{ | ||
private ShopRepository shopRepository; | ||
public ShopService(ShopRepository shopRepository){ | ||
this.shopRepository = shopRepository; | ||
} | ||
} | ||
``` | ||
|
||
2. Field λ³μ μ΄μ© | ||
|
||
```java | ||
@Service | ||
public class ShopService{ | ||
@Autowired | ||
private ShopRepository shopRepository; | ||
} | ||
``` | ||
|
||
3. setter μ΄μ© | ||
|
||
```java | ||
@Service | ||
public class ShopService{ | ||
private ShopRepository shopRepository; | ||
@Autowired | ||
public ShopService(ShopRepository shopRepository){ | ||
this.shopRepository = shopRepository; | ||
} | ||
} | ||
``` | ||
|
||
|
||
</div> | ||
</details> | ||
|
||
--- | ||
|
||
|
||
### ν΅μ¬ μ 리 | ||
|
||
- ν΄λμ€κ° λ΄λΆμ μΌλ‘ νλ μ΄μμ μμμ μμ‘΄νκ³ , κ·Έ μμμ΄ ν΄λμ€ λμμ μν₯μ μ€λ€λ©΄ μ±κΈν΄κ³Ό μ μ μ νΈλ¦¬ν° ν΄λμ€λ μ¬μ©νμ§ λ§μ. | ||
- μ΄ μμλ€μ ν΄λμ€κ° μ§μ λ§λ€κ² ν΄μλ μλλ€. | ||
- νμν μμμ (νΉμ κ·Έ μμμ λ§λ€μ΄μ£Όλ ν©ν°λ¦¬λ₯Ό) μμ±μμ (νΉμ μ μ ν©ν°λ¦¬λ λΉλμ) λ겨주μ. μμ‘΄ κ°μ²΄ μ£Όμ μ΄λΌ νλ μ΄ κΈ°λ²μ ν΄λμ€μ μ μ°μ±, μ¬μ¬μ©μ±, ν μ€νΈ μ©μ΄μ±μ κΈ°λ§νκ² κ°μ ν΄μ€λ€. |