Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
3  
the following pattern - where is it? –  back2dos Feb 17 '12 at 16:14
1  
Sorry, I had clicked on the post button before completing the question. I tried to stop the browser but it was too late. I have fixed it as quickly as possible. –  Giorgio Feb 17 '12 at 16:31
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
+1 Additionally, I would advise you to use AspectJ integrated into Spring –  Jalayn Feb 17 '12 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 '12 at 18:13
    
@Giorgio: I'm pretty sure it does. It needs dynamic proxies for lazy loading for example. –  Falcon Feb 17 '12 at 18:22
add comment

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.

share|improve this answer
    
Thanks for the information and the link. I do not think the problem in Java has to do with dynamic vs static: I imagine you can do this in a static language too, if the type system is powerful enough. I imagine it can be done in Haskell, for example, even though I am not expert enough to know how. But, of course, the dynamic language solution is interesting too. –  Giorgio Feb 18 '12 at 8:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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