코드 그라데이션

회원 도메인 - 구현과 테스트 본문

Spring/핵심 원리 구현

회원 도메인 - 구현과 테스트

완벽한 장면 2024. 1. 12. 14:13

회원 엔티티

Grade

public enum Grade { // 고객 등급
    BASIC, VIP
}

 

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;
    }
}

 

MemberRepository

public interface MemberRepository {

    void save(Member member);
    Member findById(Long memberId);

}

 

MemoryMemberRepository(구현체)

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

public interface MemberService {

    void join(Member member); // 회원가입

    Member findMember(Long memberId); // 회원조회
}

 

MemberServiceImpl(구현체)

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);
    }

}

 

 

 

 

728x90
Comments