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를 주입해 줘야 함.
그게 컨테이너 일 듯.
생성자 만들어서 컨테이너
'스프링 > 1. 스프링 핵심 원리' 카테고리의 다른 글
15강. AppConfig 리팩토링 (0) | 2023.07.16 |
---|---|
14강. 관심사의 분리 (0) | 2023.07.16 |
12강. 퍼센트 할인정책 구현 + 테스트 (0) | 2023.07.16 |
11강. 주문할인정책 테스트 (0) | 2023.07.16 |
10강. 주문할인정책 구현 (0) | 2023.07.16 |