본문 바로가기
스프링/핵심 원리

[Spring] 회원 도메인 개발

by drCode 2022. 1. 17.
728x90
반응형

회원 도메인 구조

 

인터페이스 파일

MemberRepository

MemberService

 

ENUM 파일

Grade

 

Class 파일

Member

MemberServiceImpl

MemoryMemberRepository

 

 

Grade.java 구성

package hello.core.member;

public enum Grade {
    BASIC,
    VIP
}

 

Member.java 구성

package hello.core.member;

public class Member {

    private Long id;
    private String name;
    private Grade grade;

    // 자동완성 단축키 ==> Windows : ALT + INSERT
    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.java 구성

package hello.core.member;

public interface MemberRepository {

    void save(Member member);

    Member findById(Long memberId);
}

 

MemberService.java 구성

package hello.core.member;

public interface MemberService {

    void join(Member member);

    Member findMember(Long memberId);
}

 

MemoryMemberRepository.java 구성 (MemberRepository 구현체)

package hello.core.member;

import java.util.HashMap;
import java.util.Map;

public class MemoryMemberRepository implements MemberRepository {

    // 실무에서는 ConcurrentHashMap을 사용해야함 << 동시성 문제 때문에
    // 원래는 예외처리도 해줘야함.
    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);
    }
}

 

MemberServiceImpl.java 구성 (MemberService 구현체)

package hello.core.member;

public interface MemberService {

    void join(Member member);

    Member findMember(Long memberId);
}
728x90
반응형

댓글