
IOC์ ๋ชฉ์ ์ ๋ฌ์ฑํ๊ธฐ ์ํ ํจํด ์ค DI๋ฅผ ์ ์ธํ ๋ค๋ฅธ ๋ฐฉ์๋ค์ ์ ๋ฆฌํด๋ณด์.
Template Method Pattern
Intent
- ์๊ณ ๋ฆฌ์ฆ์ ๋ผ๋๋ฅผ ์ถ์ ํด๋์ค์ Final Method๋ก ์ ์ - ํ ํ๋ฆฟ ๋ฉ์๋
- ๊ตฌ์ฒด์ ์ธ ์คํ ๋ก์ง์ ํ ํ๋ฆฟ์ ์์/ ๊ตฌํ ํ์ฌ Subclass์์ ๊ตฌํ
Intent ~ IOC
Template Method
์ ์ ์ฅ์์ ์์ ์ด ์คํ๋๋ ์์ , ํ๋ ฅ์ ๋ํ ์์กด๊ด๊ณ์ ์ฐ๊ฒฐ -Template
์ด ๊ด๋ฆฌ
Template
์ ์ ์ฅ์์ ์์ ์ ๋ก์ง์ โ์คํโ ๋ง์ ์ฑ ์์ง
- ๋ก์ง์ ๊ตฌํ(
Template Method
), ๋ก์ง์ ์์ฑ ๋ชจ๋ ์ธ๋ถ (Subclass) ๋ก ๋ถํฐ ์ ๋ฌ ๋ฐ์

c.f) Strategy Pattern ์ Template Method Pattern์ฒ๋ผ
๋ณํ์ง ์๋ ๋ถ๋ถ
๊ณผ ๋ณํ๋ ๋ถ๋ถ (๊ตฌ์ฒด ๋ก์ง)
์ ๋ถ๋ฆฌํ๋ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ๋์ง๋ง Strategy Pattern์ ์์์ด ์๋ ์์์ผ๋ก ๋ชฉ์ ์ ๋ฌ์ฑํจStrategy Pattern -
DI
๋ฅผ ํ์ฉํ IOC์ ์์ฃผ ํ์ฉ๋๋ ํจํดIntent
- ์๊ณ ๋ฆฌ์ฆ ์ ํ๊ตฐ์ ์ ์ํ๊ณ ๊ฐ๊ฐ์ ์บก์ํํ์ฌ ์ํธ ๊ตํ ๊ฐ๋ฅํ๋๋ก ๋ง๋ค์
- ํด๋ผ์ด์ธํธ์ ๋ ๋ฆฝ์ ์ผ๋ก ์๊ณ ๋ฆฌ์ฆ์ ๋ณ๊ฒฝํ ์ ์๋๋ก ํ์

- Context๊ฐ Strategy๋ฅผ ์ธ๋ถ๋ก ๋ถํฐ ์ฃผ์ ๋ฐ๋ IOC์ ์์๊ฐ ๋๋ค.
- Context๊ฐ ๊ตฌ์ฒด ์ ๋ต์ด ์๋ Strategy์๋ง ์์กดํ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ ์ด์ผ๊ธฐ

Template Callback Pattern
ํ๋ก๊ทธ๋๋ฐ์์ ์ฝ๋ฐฑ ๋๋ ์ฝ์ ํํฐ ํจ์๋ ๋ค๋ฅธ ์ฝ๋์ ์ธ์๋ก์ ๋๊ฒจ์ฃผ๋ ์คํ ๊ฐ๋ฅํ ์ฝ๋๋ฅผ ๋งํ๋ค.
- Template Method Pattern์ Template ์ ์ธ ํน์ฑ
- Strategy Pattern ์ค callback
์ด ๊ฐ์กฐ๋ ํจํด์ด๋ค.

Template (Context)๋ callback์ ํ๋๋ก ๊ฐ์ง ํ์ ์์ด ์คํ ๋ก์ง์ ์ธ์๋ก๋ง ์ ๋ฌ ๋ฐ๋๋ค.
Factory Method Pattern
Intent
- ๊ฐ์ฒด ์์ฑ์๋ํ Interface ์ ์. Subclass๊ฐ ๊ตฌ์ฒด ํ์ ์ ์ ํด์ Instantiate ํ๊ฒ ํ๊ธฐ

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
์๋น์ค๋ฅผ ๊ตฌํํ ๊ตฌ์ฒด ํด๋์ค๋ ์จ๊ธด ์ฑ๋ก ์ด๋์์๋ ์๋น์ค์ ์ ๊ทผํ ์ ์๊ฒ ํ๋ค.
์ํฐํจํด์ด๋ค.