2

I have the following pattern, which I would like to implement using some kind of generic programming, but I do not see how I can do it.

I have an interface I containing many methods with different signatures. Each method has a return type of some class type and any implementation should return null on failure, or a concrete object on success.

Now I have a concrete implementation A of I, and a decorator class D of A. For each method

T m(T1 p1, ..., Tn pn)

defined by the interface I, the implementation of m in D follows a fixed pattern:

class D
{
    private A impl = ...;

    ...

    T D.m(t1 p1, t2 p2, ..., tn pn)
    {
        init();

        T result = impl.m(p1, ..., pn);
        if (result != null)
            successCode();
        else
            failureCode();

        return result;
    }
}

The calling code uses the decorator as follows:

I instance = DecoratorFactory.createIDecorator();
T r        = instance.m(p1, ..., pn);

Question: Is there a way to implement this kind of decorator in a generic way (without writing the same boilerplate for each method)? Can this be done in Java?

If it is not possible to implement this pattern in Java, is there an elegant way to implement it in another language?

0

2 Answers 2

5

What you are probably looking for is called Aspect Oriented Programming. It works with the Proxy-Pattern, which is just what you need.

You can create a dynamic proxy for each object and tell that proxy to intercept said calls and perform your logic. All you need is one dynamic proxy class that contains your interception logic. It will wrap around any object you give it and try to perform your logic.

Start to look here:

I haven't read that articles, though. Just browse the web for the implementation/library that fits your needs best.

You might also want to check out AspectJ.

3
  • +1 Additionally, I would advise you to use AspectJ integrated into Spring
    – Jalayn
    Feb 17, 2012 at 17:37
  • Thanks, it seems this is what I need. Out of curiosity: does Hibernate use the same mechanism to implement proxies of persistent objects?
    – Giorgio
    Feb 17, 2012 at 18:13
  • @Giorgio: I'm pretty sure it does. It needs dynamic proxies for lazy loading for example.
    – Falcon
    Feb 17, 2012 at 18:22
1

It's not surprising that you couldn't see how to do this in Java. Java just doesn't support this sort of thing in any reasonable way, so solutions are complex and involve deep magic. This is a simple problem in dynamic languages.

All that is required is a handler for calls to missing methods, and way to define methods at run-time.

Then the decorator class just defines a missing method handler that will create the wrappers at run-time on the first invocation. In Ruby this requires about a dozen lines of code.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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