If there's a one-to-one mapping between the Types and calculators and
the calculators can be singletons, you could have used a MapBinder.
However you still need the CalculatorFactory or a CalculatorRegistry
(as it's not actually creating anything anymore), it's a bit smaller
and a lot less boilerplate to maintain.

public class CalculatorModule extends AbstractModule {
   protected void configure() {
     MapBinder<Types, ICalculator> mapbinder
         = MapBinder.newMapBinder(binder(), Types.class,
ICalculator.class);
     mapbinder.addBinding(Types.A).to(ACalculator.class);
     mapbinder.addBinding(Types.B).to(BCalculator.class);
     mapbinder.addBinding(Types.C).to(CCalculator.class);
   }
 }

@Singleton
public class CalculatorFactory {
    private final Map<Types, Calculator> calculators;

   @Inject
   public CalculatorFactory(Map<Types, Calculator> calculators) {
        this.calculators = calculators;
    }

    public ICalculator create(UserGroup group) {
         calculators.get(group.getType);
    }
}

On Sep 1, 10:55 pm, cpea <[email protected]> wrote:
> I don't see how this could have been done without the
> CalculatorFactory part. The User (and their group) is dynamic
> information at runtime. The "caller" doesn't know what type of
> calculator it needs.  Am I missing something here?
--~--~---------~--~----~------------~-------~--~----~
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