EEALL@ONCE

๐ŸŒฑ์„œ๋น„์Šค ๋ถ€๋ถ„ ๋กœ์ง ๋ฐ”๊พธ๊ธฐ ( OCP, DIP ์ ์šฉ ๋ฒ„์ „) ๋ณธ๋ฌธ

Spring๐ŸŒฑ

๐ŸŒฑ์„œ๋น„์Šค ๋ถ€๋ถ„ ๋กœ์ง ๋ฐ”๊พธ๊ธฐ ( OCP, DIP ์ ์šฉ ๋ฒ„์ „)

์˜ฌ์—ฃ์›์Šค 2023. 7. 11. 23:24
728x90

1. ์ด์ „ ๊ธ€ ๋ฌธ์ œ์ 

ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ์ธ OrderServiceImpl์ด ์ธํ„ฐํŽ˜์ด์Šค๋“ค ๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ, ๊ตฌ์ฒด ํด๋ž˜์Šค ( FixDiscountPolicy,RateDiscountPolicy)์—๋„ ์˜์กดํ–ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•˜์ž๋ฉด, ํ•ด๋‹น ์ฝ”๋“œ์— ๊ตฌ์ฒด ํด๋ž˜์Šค ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค๋Š” ๊ฒƒ!! 

2. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ธํ„ฐํŽ˜์ด์Šค์—๋งŒ ์˜์กดํ•˜๋„๋ก ๋งŒ๋“ค์ž.

 

public class OrderServiceImpl implements OrderService{

์•ˆ์—

private DiscountPolicy discountPolicy;

์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์„ ์–ธํ•ด์ค€๋‹ค.

๊ทผ๋ฐ ๊ทธ๋Ÿผ ๊ตฌํ˜„์ฒด๊ฐ€ ์—†๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์„๊นŒ?

3.์—ญํ•  ๋ถ„๋ฆฌ ํ›„ ์ฃผ์ž…

์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ธ๋ฌผ์ด๊ณ 
๊ตฌํ˜„์ฒด๋Š” ๋ฐฐ์šฐ๋ผ๊ณ  ๊ฐ€์ •ํ•˜์ž.

๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—ฐ๊ทน  ์ž‘๊ฐ€๋ผ๊ณ  ํ•œ๋‹ค๋ฉด,
์—ฐ๊ทน ์ž‘๊ฐ€๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์“ฐ๋ฉด์„œ ์ธ๋ฌผ์„ ๋งŒ๋“ค๊ณ  ์‚ฌ๊ฑด์„ ๋งŒ๋“ ๋‹ค.

ํ•˜์ง€๋งŒ, ๋ˆ„๊ฐ€ ์ด ์ธ๋ฌผ์˜ ์ฃผ์ธ๊ณต์„ ๋งก์„์ง€๋Š” ๋ชจ๋ฅด๋ฉฐ, ๊ฒฐ์ • ๊ถŒํ•œ์ด ์—†๋‹ค!
์ด ๊ฒฐ์ • ๊ถŒํ•œ์„ ๊ฐ€์ง„ ๊ฒƒ์ด ๋ฐ”๋กœ AppConfig๋‹ค! AppConfig๋ฅผ ์บ์ŠคํŒ… ๋””๋ ‰ํ„ฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž!

4. ๊ทธ๋ž˜์„œ ์–ด๋–ป๊ฒŒ AppConfig๋Š” ์บ์ŠคํŒ… ๋””๋ ‰ํ„ฐ๋กœ์„œ ์ผํ•˜๋Š”๊ฐ€!?

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

 AppConfig์—์„œ ์‚ฌ์šฉํ•  MemoryMemberRepository์™€ FixDiscountPolicy ( ์ฆ‰ ๋ฐฐ์šฐ๋ฅผ ์บ์ŠคํŒ…ํ•ด์ค€๋‹ค!)

public class OrderServiceImpl implements OrderService{

    private final DiscountPolicy discountPolicy;
    private final MemberRepository memberRepository;

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

๊ทธ๋Ÿฌ๋ฉด, OderServiceImpl์ด๋ผ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—ฐ๊ทน  ์ž‘๊ฐ€๋Š” ์–ด๋–ค ๋ฐฐ์šฐ๊ฐ€ ์บ์ŠคํŒ… ๋˜์—ˆ๋Š”์ง€ ๋ชจ๋ฅด๊ณ ,

๊ทน ์ค‘ ์ธ๋ฌผ๋งŒ ์‹ ๊ฒฝ์“ฐ๋ฉด ๋œ๋‹ค!

 

4. main๊ณผtest์—๋„ ์ ์šฉ!

๐Ÿฅ‘Main

public class OrderApp {
    public static void main(String[] args) {
        AppConfig appConfig = new AppConfig();
        MemberService memberService = appConfig.memberService();
        OrderService orderService = appConfig.orderService();
        long memberId = 1L;
        Member member = new Member(memberId, "memberA", Grade.VIP);
        memberService.join(member);
        Order order = orderService.createOrder(memberId, "itemA", 10000);
        System.out.println("order = " + order);

    }
}

main์—์„œ๋„ appConfig์„ ํ†ตํ•ด์„œ ๋ฐฐ์šฐ๋ฅผ ์บ์ŠคํŒ…ํ•ด์ค€๋‹ค. 

AppConfig appConfig = new AppConfig();
MemberService memberService = appConfig.memberService();
OrderService orderService = appConfig.orderService();

๐Ÿฅ‘Test

@BeforeEach
public void beforeEach(){
    AppConfig appConfig= new AppConfig();
    memberService=appConfig.memberService();
}

test๊ฐ€ ๋Œ๊ธฐ ์ „์— ๋ฌด์กฐ๊ฑด ๋จผ์ € ์‹คํ–‰ํ•˜๋Š”๊ฑฐ (@BeforeEach)์—์„œ AppConfig ์„ค์ •ํ•ด์ค€๋‹ค.

 

728x90