2 Replies Latest reply on Feb 22, 2010 9:21 AM by Pete Muir

    Decorator Rules

    Matthieu Chase Heimer Newbie

      Section 8.1.3 of the CDI spec states:


      The decorator intercepts every method:
      • declared by a decorated type of the decorator
      • that is implemented by the bean class of the decorator.


      But do the bullets imply an AND or an OR relationship? Honestly it seems like that should have been one sentence. Given:


      @Decorator
      public class DecoratorD {


          @Inject @Delegate private BizA delegate;


          public void methodA() {
              System.out.println(methodA in DecoratorD);
              delegate.methodA();
          }


          public void methodB() {
              System.out.println(methodB in DecoratorD);
          } 
      }


      Where methodA is from interface BizA and methodB is from interface BizB, should methodB in the decorator get called? My only managed bean implements both BizA and BizB but it seems like only methods from the delegate type should be used.

        • 1. Re: Decorator Rules
          Matthieu Chase Heimer Newbie

          And just to point it out. I didn't have any interfaces (decorator types) on my decorator. They don't seem to be needed for anything. Decorator methods should be limited to either the decorator types on the decorator or to the type of the delegate.

          • 2. Re: Decorator Rules
            Pete Muir Master

            The relationship is AND :-)


            Having the decorator implement the interfaces makes the contract much clearer for the developer, as it means you won't end up having a method void someSupportMethod() on the delegate type (which shouldn't be decorated, it's an implementation detail) and on the decorator (again, it's implementation detail).



            Matthieu Heimer wrote on Feb 22, 2010 06:23:


            And just to point it out. I didn't have any interfaces (decorator types) on my decorator. They don't seem to be needed for anything. Decorator methods should be limited to either the decorator types on the decorator or to the type of the delegate.


            This is a bug in Weld 1.0.0, in 1.0.1 you do need to declare an interface on the decorator (otherwise it's a definition error), and only methods declated on it will be decorated.