이것을 구현하는 데 널리 보급 된 디자인 패턴은 데코레이터 패턴입니다. Guava의 작성자가 이러한 유형의 요구 사항에 대해 앞으로 전달할 전화 번호를으로 어떻게 설계했는지 살펴볼 가치가 있습니다. 여기
는 예를 들어 추상 클래스
ForwardingListenableFuture
에서 추출 된 일부 코드 :
/**
* A {@link ListenableFuture} which forwards all its method calls to another
* future. Subclasses should override one or more methods to modify the behavior
* of the backing future as desired per the <a
* href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
*
* <p>Most subclasses can just use {@link SimpleForwardingListenableFuture}.
*
* @param <V> The result type returned by this Future's {@code get} method
*
* @author Shardul Deo
* @since 4.0
*/
public abstract class ForwardingListenableFuture<V> extends ForwardingFuture<V> implements ListenableFuture<V> {
/** Constructor for use by subclasses. */
protected ForwardingListenableFuture() {}
@Override
protected abstract ListenableFuture<V> delegate();
@Override
public void addListener(Runnable listener, Executor exec) {
delegate().addListener(listener, exec);
}
...
은 당신이 할 수있는 것처럼,이 클래스의 책임이 ListenableFuture
를 포장하고 클라이언트에 실제 구현을 숨기는 것입니다 - 모두 최종 사용자 구현자인 경우 delegate()
메서드는 구체적인 유형 대신 ListenableFuture
을 반환합니다. 실제로 당신이 랩에 요구 사항을 숨기거나 할 구현ListenableFuture
객체를 생성
코드는 포장 미래에 set
를 호출하는 클라이언트를 방지 할 수 ForwardingListenableFuture
의 구현, 그것을 "장식". javadoc가 암시하고있는 것처럼, SimpleForwardingListenableFuture
를 서브 클래스 화하면 (자), 위양 된 메소드에 잠재적 인 부작용이 도입되지 않고, 그 처리가 행해집니다.
이제 네가 언급 했으니 까. 실패한 시험 쓰기. 이것을 지적 해 주셔서 감사합니다. – tariksbl