스프링/1. 스프링 핵심 원리

13강. 퍼센트 할인정책 추가의 문제점.

sdafdq 2023. 7. 16. 14:16
public class OrderServiceImpl implements OrderService{
    private MemberRepository memberRepository = new MemoryMemberRepository();
    private final DiscountPolicy discountPolicy = new FixDscountPolicy();
    @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);
    }
}

실제 적용을 위해 private final DiscountPolicy discountPolicy = new FixDscountPolicy();

를 new RateDscountPolicy(); 로 줘야 함.

즉, OCP 위반이며 DIP(인터페이스에 의존)도 위반임.

 

public class OrderServiceImpl implements OrderService{
    private MemberRepository memberRepository = new MemoryMemberRepository();
    private DiscountPolicy discountPolicy;
    @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);
    }
}

그걸 이렇게 바꾸고 누군가 바깥에서 discountPolicy를 주입해 줘야 함.

그게 컨테이너 일 듯.

생성자 만들어서 컨테이너