Decorator Pattern: Implementing decorators using forwarding classes
March 1st, 2011 |
A forwarding class is an abstract base class which makes it easier to implement decorators for a particular interface. A forwarding class simply forwards all calls it receives to some delegate; a decorator can then be implemented by extending the forwarding class and overriding the relevant methods. Here’s an example implementation of a forwarding class for the Collection<E> interface (from Google Collections):
public abstract class ForwardingCollection<E> extends ForwardingObject implements Collection<E> {
@Override protected abstract Collection<E> delegate();
public boolean add(E element) {
return delegate().add(element);
}
// ... (more overridden methods) ...
}
Things worth noting:
- This class overrides the
delegate()method to return a more specific type. - The class contains no instance variables and does not have to be marked Serializable.
Now, here’s an example of how to implement a decorator using a forwarding class (also from Google Collections):
public class ConstrainedCollection<E> extends ForwardingCollection<E> {
private final Collection<E> delegate;
private final Constraint<? super E> constraint;
public ConstrainedCollection(Collection<E> delegate, Constraint constraint) {
this.delegate = checkNotNull(delegate);
this.constraint = checkNotNull(constraint);
}
@Override protected Collection<E> delegate() {
return delegate;
}
@Override public boolean add(E element) {
constraint.checkElement(element);
return super.add(element);
}
@Override public boolean addAll(Collection<? extends E> elements) {
return super.addAll(checkElements(elements, constraint));
}
}
This class implements a collection that checks a constraint on all elements added to the collection. Note that writing the decorator is easy given the forwarding class — only the methods relevant to the decorator need to be overridden.




