ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • SOLID 규칙 적용 (+순수 자바 버전 , spring)
    백엔드/SPRING MVC 2024. 8. 21. 10:53
    회원 예제

     

     

    기획

    • 회원 저장 로직
      1. 회원 저장
      2. 회원 조회
    • 저장 방법
      1. 메모리 저장
      2. DB 저장

     

    구조도

    SOLID 규칙 적용

    • 멤버
    import lombok.Getter;
    import lombok.Setter;
    
    @Getter
    @Setter
    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 interface MemberService {
        void join(Member member);
    
        Member findMember(Long memberId);
    }
    • 멤버 저장소 인터페이스
    public interface MemberRepository {
        void save(Member member);
    
        Member findById(Long memberId);
    }

     

    • 멤버와 멤버 저장소 연결 객체
    public class MemberServiceImpl implements MemberService{
    
        private final MemberRepository memberRepository;
    
    
        public MemberServiceImpl(MemberRepository memberRepository){
            this.memberRepository = memberRepository;
        }
    
        @Override
        public void join(Member member) {
            memberRepository.save(member);
        }
        
        @Override
        public Member findMember(Long memberId) {
            return memberRepository.findById(memberId);
        }
    }

     

    • 멤버 구현체 주입 객체 (Config)
    public class AppConfig {
    
        // 생성자 주입
        public MemberService memberService(){
            //return new MemberServiceImpl(new MemoryMemberRepository());
            return new MemberServiceImpl(new DbMemberRepository());
        }
        
        
    }

     

    • 멤버 저장소 구현체
    public class DbMemberRepository implements MemberRepository{
    
        @Override
        public void save(Member member) {
            //db 전용 save 구현
        }
    
        @Override
        public Member findById(Long memberId) {
            //db 전용 findByid 구현
            return null;
        }
    }
    
    
    public class MemoryMemberRepository implements MemberRepository{
    
        //memory에 저장
        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 static void main(String []args){
        AppConfig appConfig = new AppConfig();
        MemberService memberService = appConfig.memberService();
        
        Member member = new Member(1L, "memberA", Grade.VIP);
        memberService.join(member);
        
        Member findMember = memberService.findMember(1L);
        System.out.println("new member = "+ member.getName());
        System.out.println("find member = "+ findMember.getName());
    }

     

    순수 자버 버전 SOLID

    • 역할과 구현부를 분리하였다
      • 역할 -> MemberServiceImpl (MemberRepository)
      • 구현 -> MemoryMemberRepository,  DBMemberRepository
    • 역할에서 실행할 구현부의 부분
      • AppConfig를 이용해서 생성자 주입 (다형성)
      • 추후) 스프링에서 @Configuration을 이용해서 관리하게 됨

     

    Spring (+ @Configuration)

     

    @Configuration
    public class AppConfig {
    
        // 생성자 주입
        @Bean
        public MemberService memberService(){
            //return new MemberServiceImpl(new MemoryMemberRepository());
            return new MemberServiceImpl(new DbMemberRepository());
        }
        
        
    }
    • bean 객체를 이용해서 스프링이 관리
    • @Configuration을 지정함으로써, 스프링이 config 파일을 인식

     

    Spring 테스트

    public static void main(String []args){
        //AppConfig appConfig = new AppConfig();
        //MemberService memberService = appConfig.memberService();
        
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        applicationContext.getBean("memberService", MemberService.class); // 인터페이스에 의존 (Bean으로 관리)
        
        Member member = new Member(1L, "memberA", Grade.VIP);
        memberService.join(member);
        
        Member findMember = memberService.findMember(1L);
        System.out.println("new member = "+ member.getName());
        System.out.println("find member = "+ findMember.getName());
    }

     

    '백엔드 > SPRING MVC' 카테고리의 다른 글

    Spring Bean (+ Bean Factory)  (0) 2024.08.21
    Spring 컨테이너  (0) 2024.08.21
    Spring 역사 #3 (Spring의 탄생)  (0) 2024.08.20
    Spring 역사 #2 (POJO)  (0) 2024.08.20
    Spring 역사 #1 (EJB)  (0) 2024.08.20
Designed by Tistory.