Hello, Muhammad!

On Sat, 28 Sep 2013 07:37:37 -0300, Muhammad Gelbana <m.gelb...@gmail.com> wrote:

The last paragraph on that
page<http://tapestry.apache.org/service-advisors.html>,
states the following:

*If you do, decoration take precedence; all decorators will be in effect
before any advice (internally, they are two separate steps, with advice
being processed and the result of that used by the decorators).*


At first, it states that decoration take precedence, then it states that
internally, the advice is applied and the results is passed to the
decorator.

Is this paragraph contradicting it self ?

It's a hard to understand paragraph, maybe because the concept itself is a little hard to understand at first. When I first read it, I thought there was a contradiction, but then I thought again, a light bulb appeared over my head and I realized it does make sense.

If you have a decorator D and an advice A on service D on method m (not tested):

public interface S {
        int a(int parameter);
}

public class Implementation implements S {
        public int m(int parameter) {
                System.out.println("S.m: " + parameter);
                return parameter;
        }
}

public class D implements S {
        final private S delegate;
        public D(S delegate) {
                this.delegate = delegate;
        }
        public int m(int parameter) {
                System.out.println("D.m");
                int result = delegate.m(parameter);
                System.out.println("D.m: result: " + result);
        }
}

public class Advice implements MethodAdvice {
        public void advice(MethodInvocation invocation) {
                System.out.println("A.m");
                // calls the advised method
                invocation.proceed();
                // let's change the result value
                int result = invocation.getReturnValue();
                System.out.println("A.m: result:" + result);
                invocation.setReturnValue(result * 2);
        }
}

If we call S.a(1), the output would be this:

D.m
A.m
S.m: 1
A.m: result: 1
D.m: result: 2

The key concept here is that both service decoration and advice allow you to add code *both* before *and* after the decorated or advised original method. So the decorator takes precedence and its code is run before the advice *and* after the advice.

--
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to