회원 가입 시스템 설계
package member;
public enum Grade {
BASIC, VIP
}
package 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 member.repository;
import member.Member;
public interface MemberRepsitory {
void save(Member member);
Member findById(Long memberId);
}
package member.service;
import member.Member;
import member.repository.MemberRepsitory;
public class MemberServiceImpl implements MemberService {
private MemberRepsitory repsitory;
// 생성자 의존 주입 DI
public MemberServiceImpl(MemberRepsitory memberRepsitory) {
this.repsitory = memberRepsitory;
}
@Override
public void signUp(Member member) {
repsitory.save(member);
}
@Override
public Member findByIdMember(Long memberId) {
return repsitory.findById(memberId);
}
}
package member.repository;
import member.Member;
public class DBMemberRepositoryImpl implements MemberRepsitory {
@Override
public void save(Member member) {
// TODO Auto-generated method stub
}
@Override
public Member findById(Long memberId) {
// TODO Auto-generated method stub
return null;
}
}
package member.service;
import member.Member;
public interface MemberService {
void signUp(Member member);
Member findByIdMember(Long memberId);
}
package member.service;
import member.Member;
import member.repository.MemberRepsitory;
public class MemberServiceImpl implements MemberService {
private MemberRepsitory repsitory;
// 생성자 의존 주입 DI
public MemberServiceImpl(MemberRepsitory memberRepsitory) {
this.repsitory = memberRepsitory;
}
@Override
public void signUp(Member member) {
repsitory.save(member);
}
@Override
public Member findByIdMember(Long memberId) {
return repsitory.findById(memberId);
}
}
임시 테스트
package member;
import member.repository.DBMemberRepositoryImpl;
import member.repository.MemberRepsitory;
import member.repository.MemoryMemberRepositoryImpl;
import member.service.MemberService;
import member.service.MemberServiceImpl;
public class MainTest1 {
public static void main(String[] args) {
MemberRepsitory memberRepsitoryDB = new DBMemberRepositoryImpl();
MemberRepsitory memberRepsitory = MemoryMemberRepositoryImpl.getInstance();
MemberService memberService = new MemberServiceImpl(memberRepsitory);
// 클라이언트
Member member = new Member(100L, "티모", Grade.VIP);
// 신규 회원 가입 기능 요청
memberService.signUp(member);
// 정상적으로 메모리에 들어 갔는지 확인
System.out.println(memberService.findByIdMember(100L));
System.out.println(memberService.findByIdMember(1L));
System.out.println(memberService.findByIdMember(2L));
}
}
package order;
import member.Grade;
import member.Member;
public class FixDiscountPolicyImpl implements DiscountPolicy {
private int discountFixAmount = 1500;
@Override
public int discount(Member member, int price) {
// VIP 회원만 할인 금액 적용 된다.
if(member.getGrade() == Grade.VIP) {
return discountFixAmount;
}
return 0;
}
}
package order;
import member.Grade;
import member.Member;
public class PercentDiscountPolicyImpl implements DiscountPolicy {
private double discountPercent = 10.0;
@Override
public int discount(Member member, int price) {
if(member.getGrade() == Grade.VIP) {
return (int)(price * (discountPercent / 100));
}
return 0;
}
}
package order;
public class Order {
private Long memberId;
private String itemName;
private int imtePrice;
private int dicountPrice;
public Order(Long memberId, String itemName, int imtePrice, int dicountPrice) {
this.memberId = memberId;
this.itemName = itemName;
this.imtePrice = imtePrice;
this.dicountPrice = dicountPrice;
}
// 상품 금액에 할인된 가격 금액 출력 하는 기능
public int calculateDiscount() {
return imtePrice - dicountPrice;
}
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 getImtePrice() {
return imtePrice;
}
public void setImtePrice(int imtePrice) {
this.imtePrice = imtePrice;
}
public int getDicountPrice() {
return dicountPrice;
}
public void setDicountPrice(int dicountPrice) {
this.dicountPrice = dicountPrice;
}
@Override
public String toString() {
return "Order [memberId=" + memberId + ", itemName=" + itemName + ", imtePrice=" + imtePrice + ", dicountPrice="
+ dicountPrice + "]";
}
}
package order;
public interface OrderService {
Order createOrder(Long memberId, String itemName, int itemPrice);
}
package order;
import member.Member;
import member.repository.MemberRepsitory;
public class OrderServiceImpl implements OrderService {
private final MemberRepsitory memberRepsitory;
private final DiscountPolicy discountPolicy;
public OrderServiceImpl(MemberRepsitory memberRepsitory, DiscountPolicy discountPolicy) {
this.memberRepsitory = memberRepsitory;
this.discountPolicy = discountPolicy;
}
@Override
public Order createOrder(Long memberId, String itemName, int itemPrice) {
Member member = memberRepsitory.findById(memberId);
int discountPrice = discountPolicy.discount(member, itemPrice);
return new Order(memberId, itemName, itemPrice, discountPrice);
}
}
테스트
package order;
import member.Grade;
import member.Member;
import member.repository.MemberRepsitory;
import member.repository.MemoryMemberRepositoryImpl;
public class OrderServiceTest {
public static void main(String[] args) {
MemberRepsitory memberRepsitory = MemoryMemberRepositoryImpl.getInstance();
Member member = new Member(1L, "홍길동", Grade.VIP);
// 고정 할인 정책 테스트
DiscountPolicy fixDiscountPolicy = new FixDiscountPolicyImpl();
DiscountPolicy percentDiscountPolicy = new PercentDiscountPolicyImpl();
OrderService orderService1 = new OrderServiceImpl(memberRepsitory, percentDiscountPolicy);
Order order1 = orderService1.createOrder(1L, "맥북", 10000);
System.out.println(order1);
System.out.println(order1.calculateDiscount());
}
}
package config;
import member.repository.MemberRepsitory;
import member.repository.MemoryMemberRepositoryImpl;
import member.service.MemberService;
import member.service.MemberServiceImpl;
import order.DiscountPolicy;
import order.FixDiscountPolicyImpl;
import order.OrderService;
import order.OrderServiceImpl;
import order.PercentDiscountPolicyImpl;
public class AppConfig {
public MemberRepsitory geMemberRepsitory() {
return MemoryMemberRepositoryImpl.getInstance();
}
public MemberService getMemberService() {
return new MemberServiceImpl(geMemberRepsitory());
}
public DiscountPolicy getDiscountPolicy() {
// 할인 정책이 --> 고정할인
// 변경 --->정율 할인 바꾸면 된다.
return new FixDiscountPolicyImpl(); // 이 부분만 변경하면 됩니다.
//return new PercentDiscountPolicyImpl();
}
public OrderService getOrderService() {
return new OrderServiceImpl(geMemberRepsitory(), getDiscountPolicy());
}
}
package config;
import member.Grade;
import member.Member;
import member.service.MemberService;
import order.Order;
import order.OrderService;
public class MainApp {
public static void main(String[] args) {
AppConfig appConfig = new AppConfig();
// 클라이언트 입장에서 코딩
// 회원 가입 기능이 필요 함.
MemberService memberService = appConfig.getMemberService();
Member member = new Member(200L, "도시락", Grade.VIP);
memberService.signUp(member); // 회원 가입
// 사용자 주문 요청
OrderService orderService1 = appConfig.getOrderService();
Order order1 = orderService1.createOrder(member.getId(), "아이폰16", 3_000);
System.out.println(order1);
System.out.println(order1.getDicountPrice());
}
}
'Spring boot > 개념 공부' 카테고리의 다른 글
서비스,컨트롤러 만들어 보기 - 4 (0) | 2024.10.07 |
---|---|
레포지토리 만들기 - 3 (0) | 2024.10.07 |
Stream API (0) | 2024.10.01 |
블로그 엔티티 만들기 - 2 (0) | 2024.10.01 |
블로그 프로젝트 만들기 - 1 (6) | 2024.10.01 |