1. 회원 도메인
- 회원 가입 및 조회 가능
- 회원은 일반과 VIP로 구성
- DB는 미정
public interface MemberRepository {
void save(Member member);
Member findById(Long memberId);
}
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);}
}
- 문제점 : OCP 위반 ({new MemoryMemberRepository();} --> 구현체에 의존)
public class MemberServiceImpl implements MemberService{
private final MemberRepository memberRepository = new MemoryMemberRepository();
@Override
public void join(Member member) {memberRepository.save(member);}
@Override
public Member findMember(Long memberId) {return memberRepository.findById(memberId);}
}
2. 주문과 할인 도메인
- 회원은 상품을 주문
- 회원 등급에 따른 할인
- 할인 정책은 미정
public interface DiscountPolicy {
int discount(Member member, int price);
}
public class FixDiscountPolicy implements DiscountPolicy {
private int discountFixAmount = 1000;
@Override
public int discount(Member member, int price) {
if (member.getGrade() == Grade.VIP) {
return discountFixAmount;
} else {return 0;}
}
}
public interface OrderService {
Order createOrder(Long memberId, String itemName, int itemPrice);
}
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);
}
}