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

V1 버전의 코드 구현 #1

Open
dongjun-Yi opened this issue Mar 28, 2024 · 0 comments
Open

V1 버전의 코드 구현 #1

dongjun-Yi opened this issue Mar 28, 2024 · 0 comments

Comments

@dongjun-Yi
Copy link
Owner

dongjun-Yi commented Mar 28, 2024

  • MemberServiceImpl의 코드
public class MemberServiceImpl implements MemberService {
    private final MemberRepository memberRepository = new MemoryMemberRepository();

    @Override
    public void join(Member member) {
        memberRepository.save(member);
    }

    @Override
    public Member findMember(Long memberId) {
        return memberRepository.findById(memberId);
    }
}
  1. 코드를 보면 memberServiceImpl은 인터페이스 MemberRepository를 의존하는 것처럼 보이지만 실제 인스턴스도 같이 참조하는 것이다.
    이는 추상화에 의존해야 한다는 DIP 원칙을 위반한 것으로 위 코드는 추상된 것과 구체적인 것 두개에 모두 의존하는 코드이다.

  2. DIP 원칙 위반으로 연쇄적으로 OCP도 위반하게 된다. 그 이유는 만약 요구사항의 변경으로 repository 구현체가 다른 것으로 변경되면 코드를 수정해주어야 하기 때문이다. 만약 MemoryRepsoitory에서 다른 구현체로 바뀌게 되면 코드를 수정해 주어야 한다.

  3. 클래스의 역할을 보면 비즈니스 로직을 실행하는 역할과 memberRepository 인스턴스를 생성하는 역할을 담당해 하나의 클래스에서 2개의 책임을 갖게 된것이다. 이는 SRP 원칙에 어긋나는 코드 설계이다.


DIP, OCP, SRP 원칙을 지키기 위해 역할을 기준으로 클래스를 분리하자. 비즈니스 로직에 집중하는 클래스와 인스턴스를 생성하는 역할을 나누어 클래스를 설계하면 객체지향 설계 원칙을 지키며 코드를 구현할 수 있다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant