- AppConfig를 통해 OrderServiceImpl에 있는 DiscountPolicy에 주입해준다.
public class AppConfig {
public MemberService memberService() {
return new MemberServiceImpl(new MemoryMemberRepository());
}
public OrderService orderService() {
return new OrderServiceImpl(
new MemoryMemberRepository(),
new FixDiscountPolicy());
}
}
//중복 고려한 리팩터링
public class AppConfig {
public MemberService memberService() {
return new MemberServiceImpl(memberRepository());
}
public OrderService orderService() {
return new OrderServiceImpl(
memberRepository(),
discountPolicy());
}
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
public DiscountPolicy discountPolicy() {
return new FixDiscountPolicy();
}
}
'Spring > Basic' 카테고리의 다른 글
[Basic] IoC, DI, 컨테이너 (0) | 2024.04.03 |
---|---|
[Basic] 좋은 객체 지향 설계 원칙의 적용 (0) | 2024.04.03 |
[Basic] 설계 (0) | 2024.04.03 |
[Basic] AOP (0) | 2024.04.03 |
[Basic] 사고 방법 (0) | 2024.04.03 |