[Spring] 스프링 핵심 원리 이해1 - 주문과 할인 도메인 설계

김대준·2021년 12월 28일
0

spring

목록 보기
12/25
post-thumbnail

주문과 할인 도메인 설계


📌 주문과 할인 정책

✔ 회원은 상품을 주문할 수 있다.
✔ 회원 등급에 따라 할인 정책을 적용할 수 있다.
✔ 할인 정책은 모든 VIP는 1000원을 할인해주는 고정 금액 할인을 적용한다. (나중에 변경 될 수 있다.)
✔ 할인 정책은 변경 가능성이 높다. 회사의 기본 할인 정책을 아직 정하지 못했고, 오픈 직전까지 고민을 미루고 싶다. 최악의 경우 할인을 적용하지 않을 수도 있다. (미확정)



📌 주문 도메인 협력, 역할, 책임

주문 도메인 협력, 역할, 책임

  1. 주문 생성 : 클라이언트는 주문 서비스에 주문 생성을 요청한다.
  2. 회원 조회 : 할인을 위해서는 회원 등급이 필요하다. 그래서 주문 서비스는 회원 저장소에서 회원을 조회한다.
  3. 할인 적용 : 주문 서비스는 회원 등급에 따른 할인 여부를 할인 정책에 위임한다.
  4. 주문 결과 반환 : 주문 서비스는 할인 결과를 포함한 주문 결과를 반환한다.




📌 주문 도메인 전체

주문 도메인 전체

  • 역할과 구현을 분리해서 자유롭게 구현 객체를 조립할 수 있게 설계했다. 덕분에 회원 저장소는 물론이고, 할인 정책도 유연하게 변경할 수 있다.





📌 주문 도메인 클래스 다이어그램

주문 도메인 클래스 다이어그램





📌 주문 도메인 객체 다이어그램1

주문 도메인 객체 다이어그램1

  • 회원을 메모리에서 조회하고, 정액 할인 정책(고정 금액)을 지원해도 주문 서비스를 변경하지 않아도 된다. 역할들의 협력관계를 그대로 재사용 할 수 있다.




📌 주문 도메인 객체 다이어그램2

주문 도메인 객체 다이어그램2

  • 회원을 메모리가 아닌 실제 DB에서 조회하고, 정률 할인 정책(주문 금액에 따라 % 할인)을 지원해도 주문 서비스를 변경하지 않아도 된다.
  • 협력 관계를 그대로 재사용 할 수 있다.





주문과 할인 도메인 개발


📌 할인 정책 인터페이스

package hello.core.discount;

import hello.core.member.Member;

public interface DiscountPolicy {


    /**
    * @return 할인 대상 금액액
    */

    int discount(Member member, int price);

}



📌 정액 할인 정책 구현체

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;
        }

        return 0;
    }
}
  • VIP면 1000원 할인, 아니면 할인 없음



📌 주문 엔티티

package hello.core.Order;

public class Order {

    private Long memberId;
    private String iteamName;
    private int itemPrice;
    private int discountPrice;

    public Order(Long memberId, String iteamName, int itemPrice, int discountPrice) {
        this.memberId = memberId;
        this.iteamName = iteamName;
        this.itemPrice = itemPrice;
        this.discountPrice = discountPrice;
    }

    public int calcuatePrice(){
        return itemPrice - discountPrice;
    }

    public Long getMemberId() {
        return memberId;
    }

    public void setMemberId(Long memberId) {
        this.memberId = memberId;
    }

    public String getIteamName() {
        return iteamName;
    }

    public void setIteamName(String iteamName) {
        this.iteamName = iteamName;
    }

    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 +
                ", iteamName='" + iteamName + '\'' +
                ", itemPrice=" + itemPrice +
                ", discountPrice=" + discountPrice +
                '}';

    }
}



📌 주문 서비스 인터페이스

package hello.core.Order;

public interface OrderService {

    Order createOrder(Long memberId, String itemName, int itemPrice);

}



📌 주문 서비스 구현체

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);


        return new Order(memberId, itemName, itemPrice, discountPrice);
    }
}
  • 주문 생성 요청이 오면, 회원 정보를 조회하고, 할인 정책을 적용한 다음 주문 객체를 생성해서 반환한다.
  • 메모리 회원 리포지토리와, 고정 금액 할인 정책을 구현체로 생성한다.





주문과 할인 도메인 실행과 테스트


package hello.core.Order;

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


public class OrderServiceTest {

    MemberService memberService = new MemberServiceImpl();
    OrderService orderService = new OrderServiceImpl();

    @Test
    void createOrder(){
        Long memberId = 1L;
        Member member = new Member(memberId, "memberA", Grade.VIP);
        memberService.join(member);

        Order order = orderService.createOrder(memberId, "itemA", 1000);
        Assertions.assertThat(order.getDiscountPrice()).isEqualTo(1000);

    }

}






강의 : 스프링 핵심 원리 - 기본편

profile
kureungkureung

0개의 댓글