스프링 없는 순수한 자바로만 개발을 진행해보자
- 요구사항을 보면 회원 데이터, 할인 정책 같은 부분은 지금 결정하기 어려운 부분임
- 그렇다고 이런 정책이 결정될 때까지 개발을 무기한 기다릴 수도 없음
따라서, 인터페이스를 만들고 구현체를 언제든지 갈아끼울 수 있도록 설계하면 된다.
public enum Grade {
BASIC,
VIP
}
package hello.core.member;
public class Member {
private Long id;
private String name;
private Grade grade;
public Member(Long id, String name, Grade grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
package hello.core.member;
public interface MemberRepository {
void save(Member member);
Member findById(Long memberId);
}
package hello.core.member;
import java.util.HashMap;
import java.util.Map;
public class MemoryMemberRepository implements MemberRepository{
private static Map<Long, Member> store = new HashMap<>();
@Override
public void save(Member member) {
store.put(member.getId(), member);
}
@Override
public Member findById(Long memberId) {
return store.get(memberId);
}
}
package hello.core.member;
public interface MemberService {
void join(Member member);
Member findMember(Long memberId);
}
package hello.core.member;
public class MemberServiceImp implements MemberService{
private final MemberRepository memberRepository = new MemoryMemberRepository();
// 구현 객체를 선택해서 MemberRepository 선언
@Override
public void join(Member member) {
memberRepository.save(member);
}
@Override
public Member findMember(Long memberId) {
return memberRepository.findById(memberId);
}
}
package hello.core;
import hello.core.member.*;
public class MemberApp {
public static void main(String[] args) {
MemberService memberService = new MemberServiceImp();
Member member = new Member(1L, "memberA", Grade.VIP);
memberService.join(member);
Member findMember = memberService.findMember(1L);
System.out.println("new member = " + member.getName());
System.out.println("find Member = " + findMember.getName());
}
}
public class MemberServiceTest {
MemberService memberService = new MemberServiceImp();
@Test
void join(){
//given
Member member = new Member(1L, "memberA", Grade.VIP);
//when
memberService.join(member);
Member findMember = memberService.findMember(1L);
//then
Assertions.assertThat(member).isEqualTo(findMember);
}
}
회원 도메인 설계의 문제점
- 다른 저장소를 변경할 때 OCP 원칙을 잘 준수하고 있는가?
- OCP : 확장에는 열려 있으나 변경에는 닫혀 있어야 한다는 의미
- DIP를 잘 지키고 있나?
- 의존관계가 인터페이스 뿐만 아니라 구현까지 모두 의존하는 문제점이 있음
- DIP : 역할에 의존해야지 구현체에 의존하면 변경이 어려워진다.
역할과 구현을 분리해서 자유롭게 구현 객체를 조립할 수 있게 설계한다.
회원 저장소와 할인 정책을 유연하게 변경할 수 있다.
public interface DiscountPolicy {
/*
* @return 할인 대상 금액
* */
int discount(Member member, int price);
}
public class FixDiscountPolicy implements DiscountPolicy {
private int discountFixAmount = 1000; // 1000원 할인
@Override
public int discount(Member member, int price) {
if (member.getGrade() == Grade.VIP){
// enum 타입은 비교 타입을 ==으로 표현
return discountFixAmount;
}else{
return 0;
}
}
}
package hello.core.order;
import hello.core.member.Grade;
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 calculatorPrice(){
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 +
'}';
}
}
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);
}
}
public class OrderApp {
public static void main(String[] args) {
MemberService memberService = new MemberServiceImp();
OrderService orderService = new OrderServiceImp();
Long memberId = 1L;
Member member = new Member(memberId, "memberA", Grade.VIP);
memberService.join(member);
Order order = orderService.createOrder(memberId, "itemA", 10000);
System.out.println("order = "+ order);
System.out.println("order.calculator = "+ order.calculatorPrice());
}
}
public class OrderServiceTest {
MemberService memberService = new MemberServiceImp();
OrderService orderService = new OrderServiceImp();
@Test
void createOrder(){
Long memberId = 1L;
Member member = new Member(memberId, "memberA", Grade.VIP);
memberService.join(member);
Order order = orderService.createOrder(memberId, "itemA", 10000);
Assertions.assertThat(order.getDiscountPrice()).isEqualTo(1000);
}
}