Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Operating System.
- 10026번
- C++
- 에러핸들링
- 2475번
- BOJ
- 윤성우 저자
- n타일링2
- C#
- Operating System
- 제프리리처
- HTTP
- TCP/IP
- 운영체제
- Four Squares
- FIFO paging
- 열혈 tcp/ip 프로그래밍
- OS
- 스프링 핵심 원리
- Spring
- inflearn
- 스프링 입문
- 우아한 테크 세미나
- 우아한레디스
- 토마토
- redis
- Window-Via-c/c++
- 이펙티브코틀린
- 열혈 TCP/IP 소켓 프로그래밍
- 김영한
Archives
- Today
- Total
나의 브을로오그으
#2-4. [스프링 핵심 원리-기본편] - 주문과 할인 도메인 개발 본문
[주문과 할인 도메인 설계]
- 회원은 상품을 주문할 수 있다.
- 회원 등급에 따라 할인 정책을 적용할 수 있다.
- 할인 정책은 모든 VIP는 1000원을 할인해주는 고정 금액 할인을 적용해달라(변경 가능)
- 회사의 기본 할인 정책을 아직 정하지 못했다. 최악의 경우 할인을 정하지 않을 수 있다.(미확정)
[Project Tree]
[discount/DiscountPolicy]
package hello.core.discount;
import hello.core.member.Member;
public interface DiscountPolicy {
/**
* @return 할인 대상 금액
*/
int discount(Member member, int price);
}
[discount/FixDiscountPolicy]
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) {
return member.getGrade() == Grade.VIP ? discountFixAmount : 0;
}
}
[order/Order]
package hello.core.order;
import hello.core.discount.DiscountPolicy;
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 +
'}';
}
}
[order/OrderService]
package hello.core.order;
public interface OrderService {
Order createOrder(Long memberId, String itemName, int itemPrice);
}
[order/OrderServiceImpl]
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);
}
}
해당 설계는 꽤 괜찮은 설계이다. 할인 정책이나, 주문자의 회원조회 등의 기능들이 변경되더라도
유연하게 변경 가능하며, OrderService자체는 변경되지 않는다.
'Spring' 카테고리의 다른 글
#3-1. [스프링 핵심 원리-기본편] - 새로운 할인 정책과 문제점 (0) | 2022.07.17 |
---|---|
#2-5. [스프링 핵심 원리-기본편] - 주문과 할인 도메인 실행과 테스트 (0) | 2022.07.14 |
#2-3. [스프링 핵심 원리-기본편] - 회원 도메인 실행 및 테스트 (0) | 2022.07.13 |
#2-2. [스프링 핵심 원리-기본편] - 예제 만들기(비즈니스 요구사항과 설계 및 회원 도메인 개발) (0) | 2022.07.13 |
#2-1. [스프링 핵심 원리-기본편] - 예제 만들기(프로젝트 생성) (0) | 2022.07.13 |