スプリング基本3ビジネスニーズと設計による開発例の作成


ビジネスニーズと設計/開発の第1段階


(1)会員


1.会員登録と照会ができます.
2.会員には普通とvipの2つの等級があります.
3.会員データは独自のDBを作成でき、外部システムと連動可能(未確定)


  • Member.java
  • package springbasic.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;
        }
    }
  • Grade.java
  • package springbasic.core.member;
    
    public enum Grade {
        BASIC,
        VIP
    }
  • MemberRepository.java
  • package springbasic.core.member;
    
    public interface MemberRepository {
    
        void save(Member member);
    
        Member findById(Long memberId);
    }
  • MemoryMemberRepository.java
  • package springbasic.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);
        }
    }
  • MemberService.java
  • package springbasic.core.member;
    
    import org.springframework.stereotype.Service;
    
    public interface MemberService {
    
        void join(Member member);
    
        Member findMember(Long memberId);
    }
    
  • MemberServiceImpl.java
  • package springbasic.core.member;
    
    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);
        }
    }
  • MemberServiceTest.java
  • package springbasic.core.member;
    
    import org.assertj.core.api.Assertions;
    import org.junit.jupiter.api.Test;
    
    public class MemberServiceTest {
    
        MemberService memberService = new MemberServiceImpl();
    
        @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);
        }
    }

    (2)注文と割引政策


    1.会員は商品を注文することができます.
    2.会員等級によって優遇政策を実施することができる.
    3.優遇政策はすべてのvipに1000元の固定金額割引を要求する(後で変更する可能性がある)
    4.優遇政策は変更される可能性が高い.会社の基本的な割引政策はまだ確定していないので、開業前に延期したいという悩み.最悪の場合割引は適用されない場合があります(未定)



  • DiscountPolicy.java
  • package springbasic.core.discount;
    
    import springbasic.core.member.Member;
    
    public interface DiscountPolicy {
    
        /**
         * @return
         * */
        int discount(Member member, int price);
    }
    
  • FixDiscountPolicy.java
  • package springbasic.core.discount;
    
    import springbasic.core.member.Grade;
    import springbasic.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;
            } else {
                return 0;
            }
        }
    }
    
  • Order.java
  • package springbasic.core.order;
    
    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 +
                    '}';
        }
    }
    
  • OrderService.java
  • package springbasic.core.order;
    
    public interface OrderService {
        Order createOrder(Long memberId, String itemName, int itemPrice);
    }
    
  • OrderServiceImpl.java
  • package springbasic.core.order;
    
    import springbasic.core.discount.DiscountPolicy;
    import springbasic.core.discount.FixDiscountPolicy;
    import springbasic.core.member.Member;
    import springbasic.core.member.MemberRepository;
    import springbasic.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);
        }
    }
    
  • OrderServiceTest
  • package springbasic.core.order;
    
    import org.assertj.core.api.Assertions;
    import org.junit.jupiter.api.Test;
    import springbasic.core.member.Grade;
    import springbasic.core.member.Member;
    import springbasic.core.member.MemberService;
    import springbasic.core.member.MemberServiceImpl;
    
    public class OrderServiceTest {
    
        MemberService memberService = new MemberServiceImpl();
        OrderService orderService = new OrderServiceImpl();
    
        @Test
        void createOrder(){
    
            //given
            Long memberId = 1L;
            Member member = new Member(memberId,"memverA", Grade.VIP);
            memberService.join(member);
    
            //when
            Order order = orderService.createOrder(memberId,"itemA",10000);
    
            //then
            Assertions.assertThat(order.getDisCountPrice()).isEqualTo(1000);
        }
    }
    
  • の定理
    ->設計に基づいて開発を完了!
    -->Butは本当に良いオブジェクト向けデザインの5つの原則を守っていますか?
    ----->ソースコードが変更された場合、オープン-クローズOCPの原則を遵守しますか?