다음 예제에서는 이 CommentService에서 CommentProcessor 클래스를 사용하여 사용 사례를 구현하는 예제를 보여 준다. 서비스 메서드 sendComment()에서 CommentProcessor 클래스의 생성자를 호출하여 인스턴스를 생성하고 사용한다.
예제 5-8 서비스에서 가변 객체로 사용 사례 구현하기
@Service
public class CommentService {
public void sendComment(Comment c) {
CommentProcessor p = new CommentProcessor(); ← CommentProcessor 인스턴스를 생성한다.
p.setComment(c); ← CommentProcessor 인스턴스를 사용하여 Comment 인스턴스를 변경한다.
p.processComment(c);
p.validateComment(c); ← CommentProcessor 인스턴스를 사용하여 Comment 인스턴스를 변경한다.
c = p.getComment(); ← 수정된 Comment 인스턴스를 가져와 추가로 사용한다.
// 추가 작업 수행
}
}