메모리 회원 저장소 : 로컬에서 킬때 확인하는 용도 (서버 껐다키면 사라짐)
DB 회원 저장소 : 직접 데이터를 넣어서 회원 저장하는 용도
회원 서비스 :MemberServiceImpl
회원 저장소 : 두가지의 구현체로 나누어짐 (MemoryMemberRepository) (DbMemberRepository)
회원
회원 등급
// enum 으로 만듬
public enum Grade {
BASIC,
VIP
}
회원 엔티티
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;
}
}
회원 저장소 인터페이스
public interface MemberRepository {
void save(Member member);
Member findById(Long memberId);
}
메모리 회원 저장소 구현체
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);
}
}
회원 서비스 인터페이스
public interface MemberService {
void join(Member member);
Member findMember(Long memberId);
}
회원 서비스 구현체
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);
}
}
테스트
// test 폴더에다 작성
public class MemberSerivceTest{
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); // member와 findMember이 일치하면 true
}
}