๐ŸŒŸ๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ๐ŸŒŸ

myeonjiยท2022๋…„ 2์›” 13์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
16/22

โœจ ํ•ด๊ฒฐ๋ฐฉ๋ฒ• !! -> ๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ

์—ญํ• ๊ณผ ๊ตฌํ˜„์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์„ค๊ณ„ํ•œ ๊ฒƒ์„ ์˜ˆ๋กœ ๋“ค์–ด๋ณด๋ฉด,

  • ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค) - ๋กœ๋ฏธ์˜ค ์—ญ, ์ค„๋ฆฌ์—ฃ ์—ญ
  • ๊ตฌํ˜„(๊ตฌํ˜„์ฒด) - ๋””์นดํ”„๋ฆฌ์˜ค, ์— ๋งˆ์™“์Šจ

์œ„์ฒ˜๋Ÿผ ๋น„์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด์ „๊นŒ์ง€ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ–ˆ๋˜ ๋ฐฉ์‹์€ OrderServiceImpl์ด๋ผ๋Š” ๊ตฌํ˜„์ฒด(๋””์นดํ”„๋ฆฌ์˜ค)์—์„œ ์ง์ ‘ FixDiscountPolicy ํ˜น์€ RateDiscountPolicy(์— ๋งˆ์™“์Šจ)๋ฅผ ๋ถ€๋ฅด๊ณ  ์žˆ๋‹ค.

๋””์นดํ”„๋ฆฌ์˜ค(๊ตฌํ˜„์ฒด)๋Š” ๊ทธ์ € ๋ณธ์ธ์˜ ์—ญํ• ์ธ ๋กœ๋ฏธ์˜ค ์—ญ(์ธํ„ฐํŽ˜์ด์Šค)๋งŒ ์ž˜ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋˜๋Š”๋ฐ, ์ƒ๋Œ€์ธ ์— ๋งˆ์™“์Šจ(๊ตฌํ˜„์ฒด)๊นŒ์ง€ ๋ณธ์ธ์ด ์ •ํ•˜๊ณ  ์žˆ๋‹ค..

๋”ฐ๋ผ์„œ, ๊ณต์—ฐ ๊ธฐํš์ž๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๊ณต์—ฐ ๊ธฐํš์ž๊ฐ€ ์•Œ์•„์„œ ์•Œ๋งž์€ ์—ญํ• ์„ ๊ฐ€์ง„ ๋ฐฐ์šฐ๋“ค์„ ์ง€์ •ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

โญ ์„ค์ • ํด๋ž˜์Šค ์ƒ์„ฑ

  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ „์ฒด ๋™์ž‘์„ ๊ตฌ์„ฑ(config)ํ•˜๊ธฐ ์œ„ํ•ด, "๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ" ํ•˜๊ณ  "์—ฐ๊ฒฐ(=์ƒ์„ฑ์ž ์ฃผ์ž…)" ํ•˜๋Š” ์ฑ…์ž„์„ ๊ฐ€์ง€๋Š” ๋ณ„๋„์˜ ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค. (= ๊ณต์—ฐ๊ธฐํš์ž)
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „๋ฐ˜์˜ ๊ตฌ์„ฑ, ์šด์˜์„ ์ฑ…์ž„!

< AppConfig >

public class AppConfig {

    public MemberService memberService() {
        return new MemberServiceImpl(new MemoryMemberRepository());
    }
}

AppConfig ์˜ memberService ๋ฉ”์†Œ๋“œ์—์„œ MemberServiceImpl ์ด ์ƒ์„ฑ๋˜๋Š”๋ฐ, ๊ทธ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ MemoryMemberRepository ๋ฅผ ๋„ฃ๋Š”๋‹ค.

< MemberServiceImpl >

public class MemberServiceImpl implements MemberService {

    private final MemberRepository memberRepository;

    // ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด์„œ memberRepository์— ๋ฌด์—‡์ด ๋“ค์–ด๊ฐˆ์ง€ ๊ฒฐ์ •
    public MemberServiceImpl(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }
}

๊ทธ๋Ÿฌ๋ฉด MemberServiceImpl ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์— MemoryMemberRepository๊ฐ€ ๋“ค์–ด์˜ค๊ฒŒ ๋œ๋‹ค.

-> โญ ์ƒ์„ฑ์ž ์ฃผ์ž…!

OrderServiceImpl๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.
๊ตฌํ˜„์ฒด๋ฅผ ๋ฌด์—‡์œผ๋กœ ํ•  ๊ฒƒ์ธ์ง€๋Š” AppConfig์—์„œ ๊ฒฐ์ •ํ•œ๋‹ค.

< AppConfig >

public class AppConfig {

    public OrderService orderService() {
        return new OrderServiceImpl(new MemoryMemberRepository(), new FixDiscountPolicy());
    }
}

< OrderServiceImpl >

public class OrderServiceImpl implements OrderService {

    private final MemberRepository memberRepository;
    private final DiscountPolicy discountPolicy; // final์€ ๊ฐ’์„ ๋ฌด์กฐ๊ฑด ํ• ๋‹นํ•ด์•ผํ•จ, ๊ตฌํ˜„์ฒด์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค)์—๋งŒ ์˜์กดํ•˜๋Š” ์ค‘

    public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
        this.memberRepository = memberRepository;
        this.discountPolicy = discountPolicy;
    }

-> โญ OrderServiceImpl ๋˜ํ•œ ์ƒ์„ฑ์ž ์ฃผ์ž…!

์ž, ์ด์ œ ํ™•์ธํ•ด๋ณด์ž!!

  • MemberServiceImpl๊ณผ OrderServiceImpl์—์„œ DIP๋ฅผ ์ž˜ ์ง€ํ‚ค๊ณ  ์žˆ๋‚˜?
    ์ฆ‰, ์—ญํ• ์— ์˜์กดํ•˜๊ณ  ๊ตฌํ˜„์ฒด์—๋Š” ์˜์กดํ•˜์ง€ ์•Š๊ณ  ์žˆ๋‚˜? (= ๋‘ ๊ฐœ์˜ Impl์—์„œ ๊ตฌํ˜„์ฒด ์ฝ”๋“œ๋ฅผ ์ฐพ์•„๋ณผ ์ˆ˜ ์—†๋‹ค.)
    -> ์ž˜ ์ง€ํ‚ค๊ณ  ์žˆ๋‹ค !!

๐Ÿ“ข MemberServiceImpl๊ณผ OrderServiceImpl์€ ์ด์ œ๋ถ€ํ„ฐ "์˜์กด ๊ด€๊ณ„์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์€ ์™ธ๋ถ€(=AppConfig)" ์—๊ฒŒ ๋งก๊ธฐ๊ณ , "์‹คํ–‰์—๋งŒ ์ง‘์ค‘" ํ•˜๋ฉด ๋œ๋‹ค!!

์ด์ œ MemberServiceImpl์™€ OrderServiceImpl์˜ ์ƒ์„ฑ, MemoryMemberRepository์™€ FixDiscountPolicy ์ƒ์„ฑ์„ AppCofig ์—์„œ ํ•œ๋‹ค.

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ๊ณผ, ์‹คํ–‰ํ•˜๋Š” ์—ญํ• ์ด ๋ช…ํ™•ํžˆ ๋ถ„๋ฆฌ๋˜์—ˆ๋‹ค.

profile
DBA, ๊ฒฝ์ œ ๊ทธ๋ฆฌ๊ณ  ๊ณ ๋ƒฅ์ด

0๊ฐœ์˜ ๋Œ“๊ธ€