Not that I know of. You'd need to either push a factory into the map
which creates the instances, although then you're losing the injection
into the calculators, or something like the following. (written
inline, so may well have some typos). Issue here, I think, is that if
you're missing a dependency binding, you won't find out about it till
you call CalculatorFactory#create()
public class CalculatorModule extends AbstractModule {
protected void configure() {
MapBinder<Types, Class<ICalculator>> mapbinder
= MapBinder.newMapBinder(binder(), Types.class,
ICalculator.class);
mapbinder.addBinding(Types.A).toInstance(ACalculator.class);
mapbinder.addBinding(Types.B).toInstance(BCalculator.class);
mapbinder.addBinding(Types.C).toInstance(CCalculator.class);
}
}
@Singleton
public class CalculatorFactory {
private final Injector injector;
private final Map<Types, Class<Calculator>> calculators;
@Inject
public CalculatorFactory(Injector injector, Map<Types,
Class<Calculator>> calculators) {
this.injector = injector;
this.calculators = calculators;
}
public ICalculator create(UserGroup group) {
injector.getInstance(calculators.get(group.getType));
}
}
On Sep 8, 3:34 pm, rdcyash <[email protected]> wrote:
> I know you have mentioned but is there any possibility for each get
> from the map I can have a new instance of the class instead of
> singleton?
>
> Thanks!
>
> On Sep 3, 10:10 am, Dan Godfrey <[email protected]> wrote:
>
> > I forgot to add this link for further
> > info:http://code.google.com/p/google-guice/wiki/Multibindings
>
> > On Sep 2, 4:44 pm, cpea <[email protected]> wrote:
>
> > > That's great, thank-you! I'm going to try that... it would greatly
> > > simplify the amount of code.
>
> > > On Sep 2, 1:58 am, Dan Godfrey <[email protected]> wrote:
>
> > > > If there's a one-to-one mapping between the Types and calculators and
> > > > the calculators can be singletons, you could have used aMapBinder.
> > > > 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);
> > > > }
>
> > > > }- Hide quoted text -
>
> > - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---