728x90
반응형
먼저 할인(discount)와 주문(order)의 패키지 생성을 해야한다.
discount
Interface : DiscountPolicy
Class : FixDiscountPolitcy
order
Interface : OrderService
Class : Order, OrderServiceImpl
먼저 DiscountPolicy.java 를 생성
package hello.core.discount;
import hello.core.member.Member;
public interface DiscountPolicy {
/**
* @return 할인 대상 금액
*/
int discount(Member member, int price);
}
그 다음, FixDiscountPolicy.java를 생성한다.
package hello.core.discount;
import hello.core.member.Grade;
import hello.core.member.Member;
public class FixDiscountPolicy implements DiscountPolicy {
private int discountFixAmount = 1000; // 1000원 할인
@Override
public int discount(Member member, int price) {
if(member.getGrade() == Grade.VIP) {
return discountFixAmount;
} else {
return 0;
}
}
}
그 다음, Order.java 를 만들어준다.
package hello.core.order;
public class Order {
private Long memberId;
private String itemName;
private int itemPrice;
private int discountPrice;
public Order(Long memberId, String itemName, int itemPrice, int discountPrice) {
this.memberId = memberId;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.discountPrice = discountPrice;
}
public int calculatePrice() {
return itemPrice - discountPrice;
}
public Long getMemberId() {
return memberId;
}
public void setMemberId(Long memberId) {
this.memberId = memberId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getDiscountPrice() {
return discountPrice;
}
public void setDiscountPrice(int discountPrice) {
this.discountPrice = discountPrice;
}
@Override
public String toString() {
return "Order{" +
"memberId=" + memberId +
", itemName='" + itemName + '\'' +
", itemPrice=" + itemPrice +
", discountPrice=" + discountPrice +
'}';
}
}
생성자와 Getter And Setter, toString은 ALT + INSERT를 이용하면 간편하게 만들 수 있다.
그 다음으로, OrderService를 만든다.
package hello.core.order;
public interface OrderService {
// 주문 결과를 반환
Order createOrder(Long memberId, String itemName, int itemPrice);
}
OrderServiceImpl.java
package hello.core.order;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
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();
@Override
public Order createOrder(Long memberId, String itemName, int itemPrice) {
Member member = memberRepository.findById(memberId);
int discountPrice = discountPolicy.discount(member, itemPrice); // SRD를 잘 지킨 형식
return new Order(memberId, itemName, itemPrice, discountPrice);
}
}
discountPolicy.discount()는 딱 할인율 구하는 자기 본연의 역할만 하므로 단일 책임 원칙(SRD)에 부합한다.
728x90
반응형
'스프링 > 핵심 원리' 카테고리의 다른 글
[Spring] 새로운 할인 정책 개발 (0) | 2022.01.23 |
---|---|
[Spring] 주문과 할인 도메인 실행과 테스트 (0) | 2022.01.20 |
[Spring] 주문과 할인 도메인 설계 (0) | 2022.01.20 |
[Spring] 회원 도메인 실행과 테스트 (0) | 2022.01.17 |
[Spring] 회원 도메인 개발 (0) | 2022.01.17 |
댓글