This is how I ended up solving the problem:

public CalculatorModule {
    @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention
(RUNTIME)
    public @interface TypeA {}

    @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention
(RUNTIME)
    public @interface TypeB {}

    @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention
(RUNTIME)
    public @interface TypeC {}

    @Override
    protected void configure() {
        bind(ICalculator.class).annotatedWith(TypeA.class).to
(ACalculator.class);
        bind(ICalculator.class).annotatedWith(TypeB.class).to
(BCalculator.class);
        bind(ICalculator.class).annotatedWith(TypeC.class).to
(CCalculator.class);
    }
}

@Singleton
public class CalculatorFactory {

    private final Provider<ICalculator> a;
    private final Provider<ICalculator> b;
    private final Provider<ICalculator> c;

   @Inject
   public CalculatorFactory(@CalculatorModule.TypeA
Provider<ICalculator> AProvider,
                             @CalculatorModule.TypeB
Provider<ICalculator> BProvider,
                             @CalculatorModule.TypeC
Provider<ICalculator> CProvider) {
        a = AProvider;
        b = BProvider;
        c = CProvider;
    }

    public ICalculator create(UserGroup group) {
         if (group.getType.equals(Types.A) {
           return a.get();
        }  else if (group.getType.equals(Types.B) {
           return b.get();
        }  else if (group.getType.equals(Types.C) {
           return c.get();
        }
    }

}

and in my code.........at the top level
Guice.createInjector(new CalculatorModule());

and my class that uses the calculator factory looks like this....
public class MyClass {
    private final CalculatorFactory calcFactory;

     @Inject
     public MyClass(CalculatorFactory factory) {
          calcFactory = calcFactory;
     }

      public doSomething(User user) {
          ....
          ICalculator calc = calcFactory.create(user.getGroup());
          calc.doStuff();
     }
}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to