EEALL@ONCE

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

Spring๐ŸŒฑ

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

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

1. ์• ์ž์ผ ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ ์„ ์–ธ (๊ณ„ํš์„ ๋”ฐ๋ฅด๊ธฐ๋ณด๋‹ค ๋ณ€ํ™”์— ๋Œ€์‘ ์ˆœ์‘ํ•˜๊ธฐ๋ฅผ!)

๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๋ณด๋ฉด, ์• ์ž์ผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.

์‚ฌ์‹ค ๊ธฐํš์ž๋„ ์ž์‹ ์ด ๋ญ˜ ํ•˜๊ณ  ์‹ถ์€์ง€ ์ •ํ™•ํ•˜๊ฒŒ ๋ชจ๋ฅผ ๋•Œ๊ฐ€ ๋งŽ๋‹ค! ๊ทธ๋•Œ๋งˆ๋‹ค, ๊ฐœ๋ฐœ์ž๋Š” ๋งŒ๋“ค์–ด์„œ ๋ณด์—ฌ์ฃผ๊ณ  ๊ธฐํš์ž์—๊ฒŒ ์—ญ์œผ๋กœ ๋ฐฉ๋ฒ•์„ ์žฌ์‹œํ•œ๋‹ค๋˜๊ฐ€, ์•„๋‹˜ ๊ธฐํš์ž๊ฐ€ ์ค‘๊ฐ„์— ์ž์‹ ์ด ๊นจ๋‹ซ์ง€ ๋ชปํ•œ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ๋ณ€ํ™”๊ฒŒ ์ˆœ์‘ํ•ด์„œ ๋˜ ๊ทธ๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค.

์™œ๋ƒ๋ฉด... ๊ธฐํš์ด๋ž€ ๊ทธ๋Ÿฐ๊ฑฐ๊ณ ... ๊ฐœ๋ฐœ์ž๋ž€ ๊ทธ๊ฑธ ์‹คํ–‰์‹œํ‚ค๋Š” ์—ญํ• ์ด๋‹ˆ๊นŒ... ์–ด์ฉ” ์ˆ˜ ์—†๋‹ค.

2. ๋‹ค์‹œ ๋ฐ”๋€ ํ• ์ธ ์ •์ฑ… ์ด์•ผ๊ธฐ๋กœ ๋Œ์•„๊ฐ€์ž

๊ณ ์ • ๊ธˆ์•ก ํ• ์ธ์—์„œ
๊ณ ์ • ํผ์„ผํŠธ ํ• ์ธ์œผ๋กœ ๋ฐ”๊พผ๋‹ค. 

 

๐Ÿฅ‘ RateDiscountPolicy(%ํ• ์ธ์œจ)

package hello.core.discount;

import hello.core.member.Grade;
import hello.core.member.Member;

public class RateDiscountPolicy implements DiscountPolicy {

    private int discountPercent = 10;

    @Override
    public int discount(Member member, int price) {
       if(member.getGrade()== Grade.VIP){
           return price*discountPercent/100;
       }else{
           return 0;
        }
    }
}

๐Ÿฅ‘ OrderServiceImpl(%ํ• ์ธ์œจ)

package hello.core.order;

import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.Member;
import hello.core.member.MemberRepository;
import hello.core.member.MemoryMemberRepository;

public class OrderServiceImpl implements OrderService{

    private final MemberRepository memberRepository=new MemoryMemberRepository();
    //private final DiscountPolicy discountPolicy=new FixDiscountPolicy();
    private final DiscountPolicy discountPolicy=new RateDiscountPolicy();
    @Override
    public Order createOrder(Long memberId, String itemName, int itemPrice) {
        Member member=memberRepository.findById(memberId);
        int discountPrice=discountPolicy.discount(member, itemPrice);

        return new Order(memberId,itemName,itemPrice,discountPrice);
    }
}

๐Ÿฅ‘ TEST

package hello.core.discount;

import hello.core.member.Grade;
import hello.core.member.Member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class RateDiscountPolicyTest {

    RateDiscountPolicy discountPolicy=new RateDiscountPolicy();

    @Test
    @DisplayName("VIP๋Š” 10% ํ• ์ธ์ด ์ ์šฉ๋˜์•ผ ํ•œ๋‹ค.")
    void vip_๋งž์Œ(){
        //given
        Member member=new Member(1L,"memberVIP", Grade.VIP);
        //when
        int discount=discountPolicy.discount(member,9000);
        //then
        Assertions.assertThat(discount).isEqualTo(900);
    }

    @Test
    @DisplayName("VIP๊ฐ€ ์•„๋‹ˆ๋ฉด 10% ํ• ์ธ์ด ์ ์šฉ๋˜์ง€๋ง์•„์•ผ ํ•œ๋‹ค.")
    void vip_์•„๋‹˜(){
        //given
        Member member=new Member(1L,"memberBasic", Grade.BASIC);
        //when
        int discount=discountPolicy.discount(member,10000);
        //then
        Assertions.assertThat(discount).isEqualTo(0);
    }

}

3. ์™œ ํ•ด๋‹น ์ฝ”๋“œ๋Š” OCP, DIP ๋ฌด์‹œ๋œ ๊ฒƒ ์ผ๊นŒ?

 

 DIP: ์˜์กด๊ด€๊ณ„ ์—ญ์ „ ์›์น™ (Dependency inversion principle)

ํ”„๋กœ๊ทธ๋ž˜๋จธ๋Š” ์ถ”์ƒํ™”์— ์˜์กดํ•ด์•ผ์ง€ ๊ตฌ์ฒดํ™”์— ์˜์กดํ•˜๋ฉด ์•ˆ๋œ๋‹ค.


OrderServiceImpl ๊ฒฝ์šฐ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ํด๋ผ์ด์–ธํŠธ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด, 

์ถ”์ƒ(์ธํ„ฐํŽ˜์ด์Šค) ์˜์กด์˜ ๊ฒฝ์šฐ: DiscountPolicy์— ์˜์กดํ•˜๊ณ  ์žˆ๊ณ 
๊ตฌ์ฒด(๊ตฌํ˜„ํด๋ ˆ์Šค)์˜์กด์˜ ๊ฒฝ์šฐ: FixDiscountPolicy() -> RateDiscountPolicy()์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค.


• OCP: ๊ฐœ๋ฐฉ-ํ์‡„ ์›์น™ (Open/closed principle)

์†Œํ”„ํŠธ์›จ์–ด ์š”์†Œ๋Š” ํ™•์žฅ์—๋Š” ์—ด๋ ค์žˆ์œผ๋‚˜ ๋ณ€๊ฒฝ์—๋Š” ๋‹ซํ˜€ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ์›์น™


FixDiscountPolicy() -> RateDiscountPolicy()๋กœ ์ฝ”๋“œ๋ฅผ ํ™•์žฅํ•ด์„œ ๋ณ€๊ฒฝํ•˜๋ฉด
ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”์ค˜์•ผํ•œ๋‹ค!! 

//private final DiscountPolicy discountPolicy=new FixDiscountPolicy();
private final DiscountPolicy discountPolicy=new RateDiscountPolicy();

 


.

.

.

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด..DIP ์™€ OCP๋ฅผ ์ง€ํ‚ค๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ์„๊นŒ...? ๋‹ค์Œ๊ธ€์—์„œ ์ด์–ด ์“ฐ๋„๋ก ํ•˜๊ฒ ๋‹ค..!!

728x90