Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SRP, OCP, DIP 위반 사례 #1

Closed
CODe5753 opened this issue Apr 15, 2022 · 0 comments
Closed

SRP, OCP, DIP 위반 사례 #1

CODe5753 opened this issue Apr 15, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@CODe5753
Copy link
Owner

CODe5753 commented Apr 15, 2022

What

✉refact: 주문 시 할인 정책 변경

// private final DiscountPolicy discountPolicy = new FixDiscountPolicy();
    private final DiscountPolicy discountPolicy = new RateDiscountPolicy();

SRP 위반

한 클래스는 하나의 책임만 가져야 한다

클라이언트 객체는 직접 구현 객체를 생성, 연결, 실행하는 다양한 책임을 갖고 있음

DIP 위반

구현체가 아닌 역할에 의존

OrderServiceImpl이 interface인 DiscountPolicy를 의존하는 것 처럼 보이지만,
실제론 FixDiscountPolicy에도 의존하고 있음

OCP 위반

수정에 닫혀있고(코드 수정 X) 확장하는 것

FixDiscountPolicyRateDiscountPolicy로 변경하는 순간 OrderServiceImpl의 코드도 함께 변경해야 한다는 문제 발생
구현(class)과 역할(interface)을 분리한 것 처럼 보이지만 기존 코드를 수정했기에 완벽한 분리가 아님

Solution

  • 클라이언트인 OrderServiceImplDiscountPolicy의 구현 객체를 대신 생성하고 주입해야 한다.
    • 클라이언트 객체는 실행만, 구현 객체를 생성하고 연결하는 책임은 AppConfig가 담당하여 SRP, OCP, DIP를 지킬 수 있음
@CODe5753 CODe5753 added the bug Something isn't working label Apr 15, 2022
@CODe5753 CODe5753 changed the title OCP, DIP 위반 사례 SRP, OCP, DIP 위반 사례 Apr 15, 2022
@CODe5753 CODe5753 pinned this issue Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant