더북(TheBook)

이제 CommentService 객체의 두 의존성(CommentRepositoryCommentNotificationProxy)을 사용하여 객체 자체를 구현할 수 있다. 서비스 패키지에서 다음 예제처럼 CommentService 클래스를 작성할 수 있다.

예제 4-6 CommentService 객체 구현

public class CommentService {

    private final CommentRepository commentRepository; ← 클래스의 속성으로 의존성 두 개를 정의한다.           
    private final CommentNotificationProxy commentNotificationProxy; ← 클래스의 속성으로 의존성 두 개를 정의한다.

    public CommentService( ← 객체가 생성될 때 생성자의 매개변수로 의존성을 제공한다.
           CommentRepository commentRepository,
           CommentNotificationProxy commentNotificationProxy) {

        this.commentRepository = commentRepository;
        this.commentNotificationProxy = commentNotificationProxy;
    }

    public void publishComment(Comment comment) { ← ‘댓글 저장’과 ‘알림 전송’ 책임을 의존성에 위임하는 사용 사례를 구현한다.
        commentRepository.storeComment(comment);
        commentNotificationProxy.sendComment(comment);
    }
}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.