[refactor] 주문 금액 계산 추상화 #10
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
기존 로직
calculateOriginPrice 와 calculateDiscountPrice 가 굳이 나눠져야할까?
두 메서드 모두 order의 책임으로 주어졌기때문에 주문 금액 계산이라는 하나의 책임으로 묶어도 좋을 것 같다.
리팩토링 과정 및 문제 상황
하나의 책임으로 묶기 위해 order에 calculatePrice 라는 메서드를 생성하고
calculateOriginPrice 와 calculateDiscountPrice로 전달하는 파라미터들을 그대로 calculatePrice에 전달.
applyCouponTo 메서드에서는 사용자가 보유하고 있는 쿠폰을 반환한다.
하지만 이렇게 작성한다면 쿠폰이 있는 경우에만 정상 동작할 것이다.
쿠폰 인스턴스를 파라미터로 전달해야하지만 쿠폰이 없는 경우 어떻게 해결할 수 있을까?
해결방법 1 : null 전달
단순하게 처리한다면 null로 전달해서 처리할 수 있다.
하지만 로직마다 null 체크가 추가되어야 하고, 의도가 명확하지 않다.
해결방법 2 : NONE 쿠폰 타입 추가
현재 쿠폰 타입에는 RATE(할인율), AMOUNT(고정금액) 가 있다.
order에서 할인 금액을 계산하기 위해 coupon.calculateDiscount(originPrice)를 호출하는데,
coupon이 가지고 있는 타입에 따라 할인 금액을 계산하고 반환한다.
null을 전달하기보다 NONE 이라는 쿠폰 타입을 추가해서 의도를 명확하게 하면 어떨까
쿠폰이 없는 경우, NONE 타입을 가지는 쿠폰을 생성하여 전달한다.
null 전달보다 조금 더 객체지향적으로 리팩토링이 가능하지 않을까 생각된다.