Spring / ์์ ์๋ฐ๊ธฐ๋ฐ ์์ ( spring framework no use )
by rlaehddnd04221. ํ๋ก์ ํธ ์์ฑ
- Based on Java11 (IntelliJ IDE)
- ์คํ๋ง ๋ถํธ ์คํํฐ ์ฌ์ดํธ๋ก ์คํ๋ง ํ๋ก์ ํธ ์์ฑ
์ง๊ธ์ ์คํ๋ง์๋ ์์ํ ์๋ฐ๋ก๋ง ๊ฐ๋ฐํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ build.gradle์ ๋ฐ๋ก ๊ฑด๋ค์ง ์๋๋ค.
2. ๋น์ฆ๋์ค ์๊ตฌ์ฌํญ ์ค๊ณ
ํ์
- ํ์์ ๊ฐ์ ํ๊ณ ์กฐํํ ์ ์๋ค.
- ํ์์ ์ผ๋ฐํ์๊ณผ VIP ๋ ๊ฐ์ง ๋ฑ๊ธ์ด ์์
- ํ์ ๋ฐ์ดํฐ๋ ์์ฒด DB๋ฅผ ๊ตฌ์ถํ ์ ์๊ณ , ์ธ๋ถ DB ์์คํ
์ ์ฌ์ฉํ ์๋ ์๋ ๋ฏธํ์ ์ธ ์ํฉ
-> ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๊ณ ๊ตฌํ์ฒด๋ฅผ ์ธ์ ๋ ์ง ๊ฐ์๋ผ์ธ ์ ์๋๋ก ์ค๊ณํ๋ฉด ๋๋ค.
์ฃผ๋ฌธ๊ณผ ํ ์ธ ์ ์ฑ
- ํ์์ ์ํ์ ์ฃผ๋ฌธํ ์ ์๋ค.
- ํ์ ๋ฑ๊ธ์ ๋ฐ๋ผ ํ ์ธ ์ ์ฑ ์ ์ ์ฉํ ์ ์์
- ํ ์ธ ์ ์ฑ ์ ์ฐ์ ๋ชจ๋ VIP๋ 1000์์ ํ ์ธํด์ฃผ๋ ๊ณ ์ ๊ธ์ก ํ ์ธ์ ์ ์ฉํ ๊ฒ์ด๋ค
- ๋ค๋ฅธ ํ ์ธ ์ ์ฑ
์ ๋ณ๊ฒฝ ๊ฐ๋ฅ์ฑ์ด ๋๊ณ ์์ง ๋ฏธํ์
-> ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๊ณ ๊ตฌํ์ฒด๋ฅผ ์ธ์ ๋ ์ง ๊ฐ์๋ผ์ธ ์ ์๋๋ก ์ค๊ณํ๋ฉด ๋๋ค.
3. ํ์ ๋๋ฉ์ธ ์ค๊ณ - ๊ฐ๋ฐ - ์คํ - ํ ์คํธ (์ค๊ณ/๊ฐ๋ฐ)
ํ์ ์ํฐํฐ
ํ์ ๋ฑ๊ธ
package hello.core.member;
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 String getName() {
return name;
}
public Grade getGrade() {
return grade;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
ํ์ ์ ์ฅ์
ํ์ ์ ์ฅ์ ์ธํฐํ์ด์ค
package hello.core;
import hello.core.member.Member;
public interface MemberRepository {
void save(Member member);
Member findById(Long memberId);
}
๋ฉ๋ชจ๋ฆฌ ํ์ ์ ์ฅ์ ๊ตฌํ์ฒด
package hello.core;
import hello.core.member.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;
import hello.core.MemberRepository;
import hello.core.MemoryMemberRepository;
public class MemberServiceImp 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);
}
}
3. ํ์ ๋๋ฉ์ธ ์ค๊ณ - ๊ฐ๋ฐ - ์คํ - ํ ์คํธ (์คํ/ํ ์คํธ)
ํ์ ๋๋ฉ์ธ - ํ์ ๊ฐ์ main
package 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("findMember = " + member.getName());
System.out.println("findMember = " + findMember.getName());
}
}
์ด๋ ๊ฒ ํ ์คํธํ๋ ๊ฒ์ ์ข์ ๋ฐฉ๋ฒ์ด ์๋๋ค. JUnit ํ ์คํธ๋ฅผ ์ฌ์ฉํ์
ํ์ ๋๋ฉ์ธ - ํ์ ๊ฐ์ ํ ์คํธ
package hello.core.member;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
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);
}
}
ํ์ ๋๋ฉ์ธ ์ค๊ณ์ ๋ฌธ์
1. OCP๋ฅผ ์ ์งํค๋๊ฐ ?
2. DIP๋ฅผ ์ ์งํค๋๊ฐ? X
์์กด ๊ด๊ณ๊ฐ ์ธํฐํ์ด์ค์๋ ์์กดํ๋, ๊ตฌํ์ฒด์๊ฒ๊น์ง ์์กดํ๋ ๋ฌธ์ ์ ์ด ์์
4. ์ฃผ๋ฌธ, ํ ์ธ ๋๋ฉ์ธ ์ค๊ณ - ๊ฐ๋ฐ - ์คํ - ํ ์คํธ (์ค๊ณ/๊ฐ๋ฐ)
์ฃผ๋ฌธ๊ณผ ํ ์ธ ๋๋ฉ์ธ ์ค๊ณ
- ํ์์ ์ํ์ ์ฃผ๋ฌธํ ์ ์๋ค.
- ํ์ Grade์ ๋ฐ๋ผ ํ ์ธ ์ ์ฑ ์ ์ ์ฉํ ์ ์์
- ํ ์ธ ์ ์ฑ ์ ๋ชจ๋ VIP๋ 1000์์ ํ ์ธํด์ฃผ๋ Fixed Discount Policy๋ฅผ ์ ์ฉํ ๊ฒ.
- ํ ์ธ ์ ์ฑ ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ์ฑ์ด ๋๋ค. (๋ฏธํ์ )
์ฃผ๋ฌธ ๋๋ฉ์ธ ํ๋ ฅ, ์ญํ , ์ฑ ์
1. ์ฃผ๋ฌธ์์ฑ : ํด๋ผ์ด์ธํธ๋ ์ฃผ๋ฌธ ์๋น์ค์ ์ฃผ๋ฌธ ์์ฑ์ ์์ฒญํ๋ค.
2. ํ์์กฐํ : ์ฃผ๋ฌธ ์๋น์ค์์ MemberRepository๋ฅผ ํตํด ํ์์ ๋ํ ๋ฑ๊ธ์ ์กฐํ
3. ํ ์ธ์ ์ฉ : ํ์ธ๋ ํ์๋ฑ๊ธ์ ํตํด DiscountPolicy๋ฅผ ํตํด ํ ์ธ ์ ์ฉ
4. ์ฃผ๋ฌธ ๊ฒฐ๊ณผ ๋ฆฌํด : 3์์ ๋ฆฌํด๋ ํ ์ธ ๊ฒฐ๊ณผ๋ฅผ ํฌํจํ ์ฃผ๋ฌธ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์ธํธ์ ๋ฆฌํด
์ฃผ๋ฌธ ๋๋ฉ์ธ ์ ์ฒด
์ญํ (์ธํฐํ์ด์ค)๊ณผ ๊ตฌํ์ ๋ถ๋ฆฌํด์ ์์ ๋กญ๊ฒ ๊ตฌํ ๊ฐ์ฒด๋ฅผ ์กฐ๋ฆฝํ ์ ์๊ฒ ์ค๊ณ ํ์.
์ด๋ ๊ฒ ์ค๊ณํ๋ฉด ํ ์ธ ์ ์ฑ ์ด๋ ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ์ ์ฐํ๊ฒ ๋ณ๊ฒฝํ ์ ์๋ค.
์ฃผ๋ฌธ ๋๋ฉ์ธ ํด๋์ค ๋ค์ด์ด๊ทธ๋จ
Discount Policy (Interface)
package hello.core.discount;
import hello.core.member.Member;
public interface DiscountPolicy {
int discount(Member member, int price);
}
FixDiscountPolicy (๊ตฌํ์ฒด)
package hello.core.discount;
import hello.core.member.Grade;
import hello.core.member.Member;
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;
}
}
}
Order Entity
package hello.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 String getItemName() {
return ItemName;
}
public int getItemPrice() {
return itemPrice;
}
public int getDiscountPrice() {
return discountPrice;
}
public void setMemberId(Long memberId) {
this.memberId = memberId;
}
public void setItemName(String itemName) {
ItemName = itemName;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public void setDiscountPrice(int discountPrice) {
this.discountPrice = discountPrice;
}
@Override
public String toString() {
return "Order{" +
"memberId=" + memberId +
", ItemName='" + ItemName + '\'' +
", itemPrice=" + itemPrice +
", discountPrice=" + discountPrice +
'}';
}
}
OrderService ( Interface )
package hello.core.order;
public interface OrderService {
Order createOrder(Long memberId, String itemName, int itemPrice);
}
OrderServiceImp ( ๊ตฌํ์ฒด )
package hello.core.order;
import hello.core.MemberRepository;
import hello.core.MemoryMemberRepository;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.member.Member;
public class OrderServiceImp implements OrderService{
private final MemberRepository memberRepository = new MemoryMemberRepository();
private final DiscountPolicy dp = new FixDiscountPolicy();
@Override
public Order createOrder(Long memberId, String itemName, int itemPrice) {
Member member = memberRepository.findById(memberId);
int discountPrice = dp.discount(member,itemPrice);
return new Order(memberId, itemName, itemPrice, discountPrice);
}
}
์ฃผ๋ฌธ ์์ฑ ์์ฒญ์ด ์ค๋ฉด, ํ์ ์ ๋ณด๋ฅผ ์กฐํํ๊ณ , ํ ์ธ ์ ์ฑ ์ ์ ์ฉํ ๋ค์ ์ฃผ๋ฌธ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฆฌํดํ๋ค.
MemoryMemberRepository์ FixDiscountPolicy๋ฅผ ๊ตฌํ์ฒด๋ก ์์ฑํ๋ค.
4. ์ฃผ๋ฌธ, ํ ์ธ ๋๋ฉ์ธ ์ค๊ณ - ๊ฐ๋ฐ - ์คํ - ํ ์คํธ (์คํ/ํ ์คํธ)
์ฃผ๋ฌธ๊ณผ ํ ์ธ ์ ์ฑ ์คํ
package hello.core.order;
import hello.core.MemberRepository;
import hello.core.member.Grade;
import hello.core.member.Member;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImp;
public class OrderApp {
public static void main(String[] args) {
MemberService memberService = new MemberServiceImp();
OrderService orderService = new OrderServiceImp();
Member memberA = new Member(1L, "memberA", Grade.VIP);
memberService.join(memberA);
Order order = orderService.createOrder(1L,"itemA",10000);
System.out.println("order = " + order);
System.out.println(order.calculatePrice());
}
}
ํ ์ธ ๊ธ์ก์ด ์ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ ํ๋ฆฌ์ผ์ด์
๋ก์ง์ผ๋ก ์ด๋ ๊ฒ ํ
์คํธ ํ๋ ๊ฒ์ ์ข์ ๋ฐฉ๋ฒ์ด ์๋๋ค. JUnit ํ
์คํธ๋ฅผ ์ฌ์ฉํ์.
์ฃผ๋ฌธ๊ณผ ํ ์ธ ์ ์ฑ ํ ์คํธ
package hello.core;
import hello.core.member.Grade;
import hello.core.member.Member;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImp;
import hello.core.order.Order;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImp;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class OrderServiceTest {
MemberService memberService = new MemberServiceImp();
OrderService orderService = new OrderServiceImp();
@Test
void createOrder()
{
// given
Long memberId = 1L;
Member member = new Member(memberId,"memberA", Grade.VIP);
memberService.join(member);
// when
Order order = orderService.createOrder(memberId, "itemA", 100000);
// then
Assertions.assertThat(order.getDiscountPrice()).isEqualTo(1000);
}
}
'๐ Backend' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring / IoC, DI, Container (0) | 2023.01.30 |
---|---|
Spring / AppConfig๋ฅผ ์ด์ฉํ ๊ธฐ์กด์ OCP, DIP ๋ฌธ์ ํด๊ฒฐ (1) | 2023.01.26 |
[Spring] OOP 5๊ฐ์ง ์ค๊ณ์์น (2) | 2023.01.23 |
Spring ์ ๋ฌธ / 7. AOP (0) | 2023.01.19 |
Spring ์ ๋ฌธ / 6. DB ์ ๊ทผ ๊ธฐ์ (0) | 2023.01.19 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
Study Repository
rlaehddnd0422