- 현재 AppConfig를 보면 중복이 있고, 역할에 따른 구현이 잘 보이지 않음
기대하는 그림
AppConfig 리펙터링
package hello.core;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.member.MemberRepository;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
public class AppConfig {
public MemberService memberService() {
return new MemberServiceImpl(memberRepository());
}
private static MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
public OrderService orderService() {
return new OrderServiceImpl(memberRepository(), discountPolicy());
}
public DiscountPolicy discountPolicy() {
return new FixDiscountPolicy();
}
}
- Ctrl + Alt + M 단축키 : Extract Method
- new MemoryMemberRepository(), new FixDiscountPolicy() 부분을 Extract Method로 메소드로 만듦
- 인텔리제이에서 자동으로 중복된 부분을 변경해줌
- 역할이 드러나는 효과
- MemberService에서 구현은 MemberServiceImpl을 쓸 것이다.
- 그리고 MemberRepositroy에 대한건 MemoryMemberRepository를 쓸 것이다.
- 설계에 대한 그림이 AppConfig에서 그대로 드러남
- 역할과 구현 클래스가 한눈에 들어오게 됨
- 애플리케이션 전체 구성이 어떻게 되어 있는지 빠르게 파악 가능
※ 본 게시글은 인프런의 스프링 핵심 원리 - 기본편(김영한)을 수강하고 정리한 내용입니다.
'Study > Spring' 카테고리의 다른 글
[스프링 핵심 원리 - 기본편] 스프링 핵심 원리 이해2 - 객체 지향 원리 적용(좋은 객체 지향 설계의 5가지 원칙의 적용) (2) | 2022.12.11 |
---|---|
[스프링 핵심 원리 - 기본편] 스프링 핵심 원리 이해2 - 객체 지향 원리 적용(전체 흐름 정리) (0) | 2022.12.11 |
[스프링 핵심 원리 - 기본편] 스프링 핵심 원리 이해2 - 객체 지향 원리 적용(관심사의 분리★) (0) | 2022.12.10 |
[스프링 핵심 원리 - 기본편] 스프링 핵심 원리 이해2 - 객체 지향 원리 적용(새로운 할인 정책 적용과 문제점) (0) | 2022.12.09 |
[스프링 핵심 원리 - 기본편] 스프링 핵심 원리 이해2 - 객체 지향 원리 적용(새로운 할인 정책 개발) (0) | 2022.12.08 |