3

I am currently having to bend some framework API to fit my usecase. The framework provides generic interfaces such as

public interface Processor<T> {
    T process(T t);
}

with various implementations. These are used by some classes such as

public SomeClass<T> {
    public SomeClass(Processor<T> p, [...]) {}
}

To fit my usecase, I need SomeClass to behave differently without affecting the existing Processors. To do so, SomeClass has to operate on wrapped Ts. Hence:

SomeClass<Container<T>>

As said, this should not affect the Processors which still shall operate on T. To be able to do this I wrote delegators which unwrap T from the container and then delegate the processing of T:

public class BoxDelegatingProcessor<T> implements Processor<Container<T>> {
    private Processor<T> delegatee;

    public Container<T> process(Container<T> wrapped) {
        return wrap(delegatee.process(unwrap(wrapped));
    }
    [...]
}

What design pattern (according to GoF) is this? I don't think it's Decorator, because there es no actual "functionality" involved? It's more of a type adapter.

  • This one sounds like decorator pattern – Low Flying Pelican Feb 27 '15 at 21:53
  • I don't know what GoF pattern this is. But to me it looks like the purpose of the Processor interface is as follows: Process takes a T and returns a T, so it is possibly manipulating it in some way. The purpose of passing a Processor interface to the SomeClass constructor is to be able to change the behavior of the processing WITHOUT NEEDING TO CHANGE SOMECLASS. Instead, you would just create a new class that implements the process method. I'm a little confused at what your BoxDelegatingProcessor is attempting to do. – public wireless Feb 28 '15 at 0:36
  • Also, are you sure you need SomeClass<Container<T>>? Could you not create a List or Array of SomeClass<T> ?? – public wireless Feb 28 '15 at 0:38
  • 2
    The GOF patterns are not comprehensive. There's no guarantee that any particular technique is covered by GOF. – Robert Harvey Feb 28 '15 at 2:19
1

This is the adaptor pattern. You are adapting a Processor<T> so it can be used where a Processor<Container<T>> is expected.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.