์ธํ๋ฐ ๊น์ํ๋์ ์คํ๋ง ํต์ฌ ์๋ฆฌ - ๊ธฐ๋ณธํธ ๊ฐ์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์์ฑํ ๊ธ์ ๋๋ค.
์ด์ ๊ธ์์ ๋คํ์ฑ๋ง์ผ๋ก๋ OCP, DIP๋ฅผ ์งํฌ ์ ์๋ค๋ ๊ฒ๊น์ง ์์๋ณด์๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ OCP, DIP๋ฅผ ์งํฌ ์ ์์๊น? ๋ฐ๋ก DI์ IoC๋ฅผ ์ด์ฉํ๋ฉด ์งํฌ ์ ์๋ค. ๊ฐ๋จํ ์์๋ฅผ ํตํด ์์๋ณด์.
์ฌ๊ธฐ ์ฃผ๋ฌธ ์๋น์ค๊ฐ ์๋ค. ์ฃผ๋ฌธ ์๋น์ค๋ ํ ์ธ ์ ์ฑ ์ ๋ฐ์์ ํ ์ธ ๊ธ์ก์ ๊ณ์ฐํด์ผ ํ๋ค. ํ ์ธ ์ ์ฑ ์ ํฌ๊ฒ 2์ข ๋ฅ๊ฐ ์๋๋ฐ, ๊ณ ์ ๊ธ์ก ํ ์ธ ์ ์ฑ (FixDiscountPolicy)๊ณผ ๊ณ ์ ๋น์จ ํ ์ธ ์ ์ฑ (RateDiscountPolicy)์ด ์๋ค.
public class OrderServiceImpl {
// private final DiscountPolicy discountPolicy = new FixDiscountPolicy(); // ๊ธฐ์กด ์ฝ๋
private final DiscountPolicy discountPolicy = new RateDiscountPolicy(); // ๋ณ๊ฒฝ๋ ์ฝ๋
}
๊ณ ์ ๊ธ์ก ํ ์ธ ์ ์ฑ
์ ๊ณ ์ ๋น์จ ํ ์ธ ์ ์ฑ
์ผ๋ก ๋ณ๊ฒฝํ๋ ค๋ ์ํฉ์ด๋ผ๊ณ ๊ฐ์ ํ์. ์๋ฅผ ๋ค์ด, ์ด์ ์๋ 2000์์ ํ ์ธํด์ฃผ์๋ค๋ฉด ์ด์ ๋ ์ฃผ๋ฌธ๊ธ์ก์ 10%๋ฅผ ํ ์ธํด์ฃผ๋ ๊ฒ์ด๋ค. ์ด์ ์ฝ๋์์๋ ํ ์ธ ์ ์ฑ
์ ๊ตฌํ์ฒด๋ฅผ ๋ณ๊ฒฝํ๋ ค๋ฉด ์์ ๊ฐ์ด ํด๋ผ์ด์ธํธ์ธ OrderServiceImpl
์ ์ฝ๋๊น์ง ๋ณ๊ฒฝํด์ฃผ์ด์ผ ํ๋ค.
ํด๋ผ์ด์ธํธ(OrderServiceImpl)๊ฐ ๊ตฌํ์ฒด(FixDiscountPolicy)์๋ ์์กดํ๊ธฐ ๋๋ฌธ์ ์์ ๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ๊ทธ๋ ๋ค๋ฉด ๊ตฌํ์ฒด์ ์์กดํ๋ ๋ถ๋ถ์ ๋นผ๋ฉด ๋์ง ์์๊น?
public class OrderServiceImpl {
private DiscountPolicy discountPolicy;
}
DIP๋ฅผ ์๋ฐํ์ง ์๋๋ก, ํด๋ผ์ด์ธํธ๊ฐ ์ธํฐํ์ด์ค์๋ง ์์กดํ๊ฒ๋ ์์กด๊ด๊ณ๋ฅผ ๋ณ๊ฒฝํ๋ค. (final ํค์๋๊ฐ ์์ผ๋ฉด ๋ฐ๋์ ๊ฐ์ ํ ๋นํด์ค์ผ ํ๋ฏ๋ก ํจ๊ป ์ง์์ฃผ์๋ค.)
๊ทธ๋ฐ๋ฐ ์์ ๊ฐ์ด ์ฐธ์กฐ๋ณ์๋ง ์ ์ธํ๋ฉด ์ฃผ๋ฌธ ์๋น์ค์์๋ ์๋ฌด๊ฒ๋ ํ ์ ์๋ค. ๋๊ตฐ๊ฐ๋ ๋ฐ๋์ ํ ์ธ ์ ์ฑ ์ ๊ตฌํ์ฒด๋ฅผ ์์ฑํด์ ํด๋ผ์ด์ธํธ์๊ฒ ๊ฐ๋ค์ฃผ์ด์ผ ํ๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด์ ๊ทธ '๋๊ตฐ๊ฐ'์ ํด๋นํ๋, ์ค์ ํด๋์ค๋ฅผ ๋ง๋ค์ด ๋ณผ ์ฐจ๋ก์ด๋ค.
1. ์ค์ ํด๋์ค ์์ฑ
์ค์ ํด๋์ค๋ ๊ตฌํ์ฒด๋ฅผ ์์ฑํ ๋ค, ํด๋ผ์ด์ธํธ์ ๊ตฌํ์ฒด๋ฅผ ์ฐ๊ฒฐํด์ค๋ค.
์ด์ ์๋ OrderServiceImpl
์ด ์ง์ ๊ตฌํ์ฒด๋ฅผ ์ ํํ๋ค๋ฉด, ์ด์ ๋ AppConfig
๋ผ๋ ์ค์ ํด๋์ค๊ฐ ๊ตฌํ์ฒด๋ฅผ ์ ํํ ๋ค ์์ฑ์๋ฅผ ํตํด OrderServiceImpl
์๊ฒ ๊ตฌํ์ฒด๋ฅผ ์ ๋ฌํด ์ค ๊ฒ์ด๋ค.
public class OrderServiceImpl implements OrderService {
private DiscountPolicy discountPolicy;
public OrderServiceImpl(DiscountPolicy discountPolicy) {
this.discountPolicy = discountPolicy;
}
}
public class AppConfig {
public OrderService orderService() {
return new OrderServiceImpl(new FixDiscountPolicy());
}
}
์์ ์ฝ๋์์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ฐ๊ฒฐํด์ฃผ๋ ๊ฒ์ AppConfig
๊ฐ ๋ด๋นํ๊ณ , OrderServiceImpl
์ ์ด๋ค ๊ตฌํ์ฒด๊ฐ ๋ค์ด์ค๋์ง ์์ง ๋ชปํ ์ฑ๋ก, AppConfig
๊ฐ ๊ฐ๋ค์ค ๊ตฌํ์ฒด๋ฅผ ๋ฐ์๋ค ์ฌ์ฉํ๊ธฐ๋ง ํ๋ค.
๋ง์ฝ ํ ์ธ ์ ์ฑ ์ ๊ณ ์ ๋น์จ ํ ์ธ ์ ์ฑ ์ผ๋ก ๋ณ๊ฒฝํ๊ณ ์ถ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
public class AppConfig {
public OrderService orderService() {
return new OrderServiceImpl(new RateDiscountPolicy());
}
}
AppConfig
์ FixDiscountPolicy()
๋ฅผ RateDiscountPolicy()
๋ก ๋ณ๊ฒฝํ๋ฉด ๋๋ค. ์ด์ ๊ณผ ๋ฌ๋ฆฌ, ํ ์ธ ์ ์ฑ
์ด ๋ณ๊ฒฝ๋์ด๋ OrderServiceImpl
์ ์ํฅ์ ๋ฐ์ง ์๋๋ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด์ฒ๋ผ AppConfig
์ ๋ฑ์ฅ์ผ๋ก ์ ํ๋ฆฌ์ผ์ด์
์ด ์ค์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๋ถ๋ถ๊ณผ, ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ฐ๊ฒฐํ๋ ๋ถ๋ถ์ผ๋ก ๋ถ๋ฆฌ๋์๋ค.
์ด์ ์ค์ ํด๋์ค๋ฅผ ๋ถ๋ฆฌํจ์ ๋ฐ๋ผ ๋ฐ๋ ๋ถ๋ถ์ ๋ํด ์์๋ณด์.
2. IoC (Inversion Of Control, ์ ์ด์ ์ญ์ )
์ด์ ์๋ ํด๋ผ์ด์ธํธ ๊ตฌํ์ฒด์ธ OrderServiceImpl
์ด ์ค์ค๋ก ํ ์ธ ์ ์ฑ
๊ตฌํ์ฒด๋ฅผ ์ ํํ๊ณ ์ฌ์ฉํ๋ค. ๊ตฌํ์ฒด๋ค๋ผ๋ฆฌ ํ๋ก๊ทธ๋จ์ ์ ์ดํ ๊ฒ์ด๋ค.
ํ์ง๋ง AppConfig์ ๋ฑ์ฅ์ผ๋ก ์ธํด ๊ตฌํ์ฒด๋ ํ ์ธ ์ ์ฑ
์ ๋ฐ์๋ค ์ฌ์ฉํ๊ธฐ๋ง ํ๊ณ , ํ๋ก๊ทธ๋จ์ ์ ์ดํ๋ ๋ถ๋ถ(=๊ตฌํ์ฒด๋ฅผ ์ ํํ๊ณ ์์ฑํ๋ ๋ถ๋ถ)์ AppConfig์๊ฒ ๋์ด๊ฐ๋ค. OrderServiceImpl
์ ์ด๋ค ๊ตฌํ์ฒด๊ฐ ๋ค์ด์ฌ์ง ์ ์ ์๋ค.
์ด๋ ๊ฒ ํ๋ก๊ทธ๋จ์ ์ ์ด ํ๋ฆ์ ์ธ๋ถ์์ ๊ด๋ฆฌํ๋ ๊ฒ์ ์ ์ด์ ์ญ์ (IoC)์ด๋ผ๊ณ ํ๋ค. ์คํ๋ง์์ ์ ์ด์ ์ญ์ ์ด ์ผ์ด๋๋ค๋ ๊ฒ์, ์คํ๋ง์ด ํ๋ก๊ทธ๋จ์ ํ๋ฆ์ ์ ์ดํ๋ค๋ ์๋ฏธ์ด๊ธฐ๋ ํ๋ค.
๐กํ๋ ์์ํฌ vs ๋ผ์ด๋ธ๋ฌ๋ฆฌ
์ ์ด์ ์ญ์ ์ ํ๋ ์์ํฌ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ตฌ๋ถํ๋ ๊ธฐ์ค์ด๊ธฐ๋ ํ๋ค. ํ๋ ์์ํฌ์์๋ ์ ์ด์ ์ญ์ ์ด ์ผ์ด๋ ํ๋ ์์ํฌ๊ฐ ์ฝ๋๋ฅผ ์ ์ดํ๊ณ ๋์ ์คํํด์ค๋ค. ํ์ง๋ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์๋ ์ ์ด์ ์ญ์ ์ด ์ผ์ด๋์ง ์์, ๋ด๊ฐ ์์ฑํ ์ฝ๋๊ฐ ์ง์ ํ๋ก๊ทธ๋จ ์ ์ด์ ํ๋ฆ์ ๋ด๋นํ๋ค.
3. DI (Dependency Injection, ์์กด๊ด๊ณ ์ฃผ์ )
์์กด๊ด๊ณ ์ฃผ์ ์ด๋, ์ ํ๋ฆฌ์ผ์ด์ ์คํ ์์ ์ ์ธ๋ถ์์ ์ค์ ๊ตฌํ ๊ฐ์ฒด๋ฅผ ์์ฑํด ํด๋ผ์ด์ธํธ์ ์ ๋ฌํ๋ ๊ฒ์ ๋งํ๋ค. ์์กด๊ด๊ณ ์ฃผ์ ์ ์ด์ฉํ๋ฉด, ์ ์ ์ธ ํด๋์ค ์์กด๊ด๊ณ๋ฅผ ๋ณ๊ฒฝํ์ง ์๊ณ ๋์ ์ธ ์ธ์คํด์ค ์์กด๊ด๊ณ๋ฅผ ์ฝ๊ฒ ๋ณ๊ฒฝํ ์ ์๋ค.
์ด๋ ๊ฒ ๋งํ๋ฉด ๊ฐ์ด ์ ์์ฌํ ๋ ์ฝ๋์ ๊ทธ๋ฆผ์ ํตํด ์ดํด๋ณด์.
์์์ ๋ดค๋ ๊ทธ๋ฆผ์ด ๋ง๋ค. ํด๋์ค ์์กด๊ด๊ณ๋ ์ ํ๋ฆฌ์ผ์ด์
์ ์คํํ์ง ์์๋ ํ์
ํ ์ ์๋ค. (๊ทธ๋์ ์ ์ ์ด๋ผ๊ณ ํ๋ค.)OrderServiceImpl
์ OrderService
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์๊ณ , DiscountPolicy
์ธํฐํ์ด์ค์ ์์กดํ๊ณ ์๋ค. ๊ทธ๋ฆฌ๊ณ DiscountPolicy
๋ FixDiscountPolicy
์ RateDiscountPolicy
๋ผ๋ 2๊ฐ์ ๊ตฌํ ํด๋์ค๋ฅผ ๊ฐ๊ณ ์๋ค.
๊ทธ๋ฐ๋ฐ ์ ์ ์ธ ํด๋์ค ์์กด๊ด๊ณ๋ก๋, ์คํ ์์ ์ OrderServiceImpl
์ด ์ด๋ค ๊ตฌํ์ฒด๋ฅผ ์ฃผ์
๋ฐ๋์ง ์ ์๊ฐ ์๋ค. ์ด๊ฒ์ด ๋ฐ๋ก ํด๋์ค ์์กด๊ด๊ณ์ ์ธ์คํด์ค ์์กด๊ด๊ณ์ ์ฐจ์ด์ด๋ค. ์ ํ๋ฆฌ์ผ์ด์
์ ์คํํ์ฌ ์ค์ ๊ตฌํ์ฒด๊ฐ ์์ฑ๋ ํ ๊ทธ๋ฆฐ ์ธ์คํด์ค ๊ฐ์ ์์กด๊ด๊ณ๊ฐ ๋ฐ๋ก ์ธ์คํด์ค ์์กด๊ด๊ณ์ด๋ค.
public class AppConfig {
public OrderService orderService() {
return new OrderServiceImpl(discountPolicy());
}
public DiscountPolicy discountPolicy() {
return new FixDiscountPolicy();
}
}
์ญํ ๊ณผ ๊ตฌํ์ด ์ ๋ณด์ด๊ฒ๋ ์์ ํ ์ค์ ํ์ผ์ด๋ค. ์์ ์ฝ๋๋ฅผ ๋ฐ๋ผ ์ธ์คํด์ค ์ฌ์ด์ ์์กด๊ด๊ณ๋ฅผ ๊ทธ๋ ค๋ณด๋ฉด ์๋์ ๊ฐ์ด ๊ทธ๋ ค์ง ๊ฒ์ด๋ค.
๋ง์ฝ ์ค์ ํ์ผ์์ FixDiscountPolicy
๊ฐ RateDiscountPolicy
๋ก ๋ณ๊ฒฝ๋์ด๋, ํด๋์ค ์์กด๊ด๊ณ๋ ๋ณํ์ง ์๊ณ ์ธ์คํด์ค ์์กด๊ด๊ณ๋ง ๋ณํ๋ค. ์ด๊ฒ ๋ฐ๋ก DI์ ์ฅ์ ์ด๋ค.
์ง๊ธ๊น์ง DI์ IoC์ ๋ํด ์์๋ณด์๋ค. AppConfig์ฒ๋ผ DI์ IoC๋ฅผ ํด์ฃผ๋ ๊ฒ์ DI ์ปจํ ์ด๋๋ผ๊ณ ํ๋๋ฐ, DI ์ปจํ ์ด๋๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ฉด์ ์ฐ๊ฒฐํด์ค๋ค. ์คํ๋ง์ ๋ฐฐ์๋ณธ ์ ์ด ์๋ ์ฌ๋์ด๋ผ๋ฉด ์คํ๋ง์ ํน์ง์ผ๋ก DI์ IoC๋ฅผ ๋ง์ด ๋ค์ด๋ดค์ํ ๋ฐ, ์คํ๋ง์ด ๋ฐ๋ก DI ์ปจํ ์ด๋ ์ญํ ์ ํด์ฃผ๊ธฐ ๋๋ฌธ์ด๋ค.
4. ์ ๋ฆฌ
- IoC(Inversion of Control, ์ ์ด์ ์ญ์ ): ํ๋ก๊ทธ๋จ์ ์ ์ด ํ๋ฆ์ ์ธ๋ถ์์ ๊ด๋ฆฌํ๋ ๊ฒ
- DI(Dependency Injection, ์์กด๊ด๊ณ ์ฃผ์ ): ์ ํ๋ฆฌ์ผ์ด์ ์คํ ์์ ์ ์ธ๋ถ์์ ์ค์ ๊ตฌํ ๊ฐ์ฒด๋ฅผ ์์ฑํด ํด๋ผ์ด์ธํธ์ ์ ๋ฌํ๋ ๊ฒ
- DI ์ปจํ ์ด๋: DI๋ฅผ ํด์ฃผ๋ ์ปจํ ์ด๋. ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ฉด์ ์ฐ๊ฒฐํด์ค
'๐๐ฉ๐ซ๐ข๐ง๐ > ๐๐๐ฌ๐ข๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ปดํฌ๋ํธ ์ค์บ (0) | 2023.09.02 |
---|---|
์คํ๋ง์์์ ์ฑ๊ธํค ํจํด (0) | 2023.09.02 |
์คํ๋ง ์ปจํ ์ด๋๋? (0) | 2023.09.02 |
๊ฐ์ฒด ์งํฅ ์ค๊ณ์ 5๊ฐ์ง ์์น (0) | 2023.08.28 |
Spring๊ณผ ๊ฐ์ฒด์งํฅ์ ๋ํ์ฌ (0) | 2023.08.28 |