이 인터페이스는 CommentService 객체가 댓글 저장이라는 사용 사례를 구현하는 데 필요한 기능만 제공한다. 이 계약을 구현하는 객체를 정의할 때는 storeComment(Comment comment) 메서드를 재정의하여 구현 방법을 정의해야 한다. 다음 코드에서 DBCommentRepository 클래스의 정의를 확인할 수 있다. 아직 데이터베이스에 연결하는 방법을 알지 못하므로 이 작업을 시뮬레이션하려고 콘솔에 텍스트만 출력해 볼 것이다. 12장부터 애플리케이션을 데이터베이스에 연결하는 방법도 배울 것이다.
예제 4-3 CommentRepository 인터페이스 구현하기
public class DBCommentRepository implements CommentRepository {
@Override
public void storeComment(Comment comment) {
System.out.println("Storing comment: " + comment.getText());
}
}