๐Ÿ—จ๏ธ

IOC & DI & Others

notion image
IOC์˜ ๋ชฉ์ ์„ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํŒจํ„ด ์ค‘ DI๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ๋ฐฉ์‹๋“ค์„ ์ •๋ฆฌํ•ด๋ณด์ž.

Template Method Pattern

Intent

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ผˆ๋Œ€๋ฅผ ์ถ”์ƒ ํด๋ž˜์Šค์˜ Final Method๋กœ ์ •์˜ - ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ
  • ๊ตฌ์ฒด์ ์ธ ์‹คํ–‰ ๋กœ์ง์€ ํ…œํ”Œ๋ฆฟ์„ ์ƒ์†/ ๊ตฌํ˜„ ํ•˜์—ฌ Subclass์—์„œ ๊ตฌํ˜„

Intent ~ IOC

  • Template Method์˜ ์ž…์žฅ์—์„œ ์ž์‹ ์ด ์‹คํ–‰๋˜๋Š” ์‹œ์ , ํ˜‘๋ ฅ์— ๋Œ€ํ•œ ์˜์กด๊ด€๊ณ„์˜ ์—ฐ๊ฒฐ - Template์ด ๊ด€๋ฆฌ
  • Template์˜ ์ž…์žฅ์—์„œ ์ž์‹ ์€ ๋กœ์ง์˜ โ€˜์‹คํ–‰โ€™ ๋งŒ์„ ์ฑ…์ž„์ง
  • ๋กœ์ง์˜ ๊ตฌํ˜„(Template Method), ๋กœ์ง์˜ ์ƒ์„ฑ ๋ชจ๋‘ ์™ธ๋ถ€ (Subclass) ๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ ๋ฐ›์Œ
template method pattern uml
template method pattern uml

c.f) Strategy Pattern ์€ Template Method Pattern์ฒ˜๋Ÿผ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„๊ณผ ๋ณ€ํ•˜๋Š” ๋ถ€๋ถ„ (๊ตฌ์ฒด ๋กœ์ง) ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€๋งŒ Strategy Pattern์€ ์ƒ์†์ด ์•„๋‹Œ ์œ„์ž„์œผ๋กœ ๋ชฉ์ ์„ ๋‹ฌ์„ฑํ•จ
Strategy Pattern - DI๋ฅผ ํ™œ์šฉํ•œ IOC์— ์ž์ฃผ ํ™œ์šฉ๋˜๋Š” ํŒจํ„ด
Intent
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ œํ’ˆ๊ตฐ์„ ์ •์˜ํ•˜๊ณ  ๊ฐ๊ฐ์„ ์บก์Аํ™”ํ•˜์—ฌ ์ƒํ˜ธ ๊ตํ™˜ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ค์ž
  • ํด๋ผ์ด์–ธํŠธ์™€ ๋…๋ฆฝ์ ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์ž
strategy pattern uml
strategy pattern uml
  • Context๊ฐ€ Strategy๋ฅผ ์™ธ๋ถ€๋กœ ๋ถ€ํ„ฐ ์ฃผ์ž…๋ฐ›๋Š” IOC์˜ ์˜ˆ์‹œ๊ฐ€ ๋œ๋‹ค.
  • Context๊ฐ€ ๊ตฌ์ฒด ์ „๋žต์ด ์•„๋‹Œ Strategy์—๋งŒ ์˜์กดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•œ ์ด์•ผ๊ธฐ
 
Strategy Pattern & Dependency Injection
Strategy Pattern & Dependency Injection

Template Callback Pattern

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์ฝœ๋ฐฑ ๋˜๋Š” ์ฝœ์• ํ”„ํ„ฐ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ์ธ์ˆ˜๋กœ์„œ ๋„˜๊ฒจ์ฃผ๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ๋งํ•œ๋‹ค.
ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์ฝœ๋ฐฑ ๋˜๋Š” ์ฝœ์• ํ”„ํ„ฐ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ์ธ์ˆ˜๋กœ์„œ ๋„˜๊ฒจ์ฃผ๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋ฅผ ๋งํ•œ๋‹ค.
  • Template Method Pattern์˜ Template ์ ์ธ ํŠน์„ฑ
  • Strategy Pattern ์ค‘ callback
์ด ๊ฐ•์กฐ๋œ ํŒจํ„ด์ด๋‹ค.
 
Template Callback Pattern
Template Callback Pattern
Template (Context)๋Š” callback์„ ํ•„๋“œ๋กœ ๊ฐ€์งˆ ํ•„์š” ์—†์ด ์‹คํ–‰ ๋กœ์ง์˜ ์ธ์ž๋กœ๋งŒ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค.

Factory Method Pattern

Intent

  • ๊ฐ์ฒด ์ƒ์„ฑ์—๋Œ€ํ•œ Interface ์ •์˜. Subclass๊ฐ€ ๊ตฌ์ฒด ํƒ€์ž…์„ ์ •ํ•ด์„œ Instantiate ํ•˜๊ฒŒ ํ•˜๊ธฐ
notion image
interface Product { } abstract class Factory { protected abstract Product factoryMethod(); } class ConcreteProduct implements Product { } class ConcreteFactory extends Factory { protected Product factoryMethod() { return new ConcreteProduct(); } } public class Client { public static void main(String[] args) { Factory creator = new ConcreteFactory(); Product product = creator.factoryMethod(); System.out.println(product.getClass().getSimpleName()); } }

Intent ~ IOC

Factory๋กœ ์„ ์–ธ๋œ creator ๋ณ€์ˆ˜๋Š” factoryMethod๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์‹œ์ ์— ์ž์‹ ์ด ์–ด๋–ค Product๋ฅผ ์ƒ์„ฑํ•  ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.
์œ„ ์ฝ”๋“œ์—์„œ๋Š” ์„ ์–ธ๋‹น์‹œ ํ• ๋‹น๋ฐ›์€ ConcreteFactory์˜ ๋กœ์ง์ด ๋™์ž‘ ํ•˜์ง€๋งŒ ์ค‘๊ฐ„์— ๋‹ค๋ฅธ ๋กœ์ง์œผ๋กœ ๋Œ€์ฒด ๋˜์—ˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค.
์ฆ‰ Factory์˜ ์ž…์žฅ์—์„œ ์‹คํ–‰์‹œ์ ์— ์ž์‹ ์ด ์–ด๋–ค Product๋ฅผ ์ƒ์„ฑํ•  ์ง€๋Š” ์ „ํ˜€ ์•Œ ์ˆ˜ ์—†๋‹ค. Client๊ฐ€ ์ž์‹ ์—๊ฒŒ ํ• ๋‹นํ•œ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ๋งŒ์„ ์ฑ…์ž„์งˆ ๋ฟ์ด๋‹ค.

Service Locator Pattern

 
public class MemberService { private final JsonParser jsonParser; public MemberService() { this.jsonParser = ServiceLocator.jsonParser(); } public String parseString(Member member) { return this.jsonParser.parseString(member); } }
public class ServiceLocator { public static JsonParser jsonParser() { //๊ฒฝ์šฐ์— ๋”ฐ๋ผ Singleton์ด๋‚˜ ๋‹ค๋ฅธ Scope๋กœ ๊ตฌํ˜„์„ ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. return new JacksonParser(); } }

Intent

์„œ๋น„์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ตฌ์ฒด ํด๋ž˜์Šค๋Š” ์ˆจ๊ธด ์ฑ„๋กœ ์–ด๋””์—์„œ๋‚˜ ์„œ๋น„์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
 
์•ˆํ‹ฐํŒจํ„ด์ด๋‹ค.