You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Muhammad Gelbana <m....@gmail.com> on 2013/09/28 12:37:37 UTC

Advising and Decoration order

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 ?
*---------------------*
*Muhammad Gelbana*
http://eg.linkedin.com/in/mgelbana/

Re: Advising and Decoration order

Posted by Muhammad Gelbana <m....@gmail.com>.
This is VERY flexible ! And tricky too.

Thanks a lot for your time :)

*---------------------*
*Muhammad Gelbana*
http://eg.linkedin.com/in/mgelbana/


On Mon, Sep 30, 2013 at 1:41 PM, Thiago H de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> Hello, Muhammad!
>
>
> On Sat, 28 Sep 2013 07:37:37 -0300, Muhammad Gelbana <m....@gmail.com>
> wrote:
>
>  The last paragraph on that
>> page<http://tapestry.apache.**org/service-advisors.html<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-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Advising and Decoration order

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
Hello, Muhammad!

On Sat, 28 Sep 2013 07:37:37 -0300, Muhammad Gelbana <m....@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-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org