2 Replies Latest reply on Jan 6, 2010 7:06 AM by Ian R

    Factory pattern and Producer methods

    Ian R Newbie

      I've been trying to reproduce a simple Factory pattern using a producer but I am running into some problems. Maybe somebody could give me a hint as to what I'm doing wrong, I'm barking up the wrong tree or tell me I'm an idiot for even trying this!


      I want to inject an interface into a class. I don't want to write a Factory class that people call to get it, I'd like CDI to automatically produce the correct class for me. So I've written four classes:
      1. Interface - which is the interface I want to produce.
      2. InterfaceFactory. The only method is one which is a @Produces method which returns Interface. This class decides which implementation to return (details don't really matter).
      3. InterfaceImpl1 and InterfaceImpl2. Both with package scope constructors - I can change this, but I didn't want them to be public.


      The issue I run into is that Weld finds an ambiguity about what to inject - it finds the producer method, and the class implementations. I thought that not making the constructor public would mean Weld couldn't construct it and hence there's no ambiguity.


      I can fix this by creating a new annotation and have the producer return one of those, but this doesn't help a lot.


      Can I do what I'm trying to do without the need for the extra annotation?


      Thanks,


      Ian