I'm quite new to Guice so perhaps I'm doing things wrong but I use this pattern a lot:
@Inject
public MyClass(@Named("LowPricingStrategyA") IPricingStrategyA pricingStrategyA,
ICalculator calculator) {
this.pricingStrategyA = pricingStrategyA ;
this.calculator = calculator;
}
then in the module:
protected void configure() {
bind(
IPricingStrategyA.class).annotatedWith(Names.named("LowPricingStrategyA")).to(LowPricingStrategyA.class);
bind( IPricingStrategyA.class).annotatedWith(Names.named("HighPricingStrategyA")).to(HighPricingStrategyA.class);
bind(
ICalculator.class).to(APRCalculator.class);
...
Note that for IPricingStrategyA I have two possible bindings LowPricingStrategyA & HighPricingStrategyA, the module doesn't know/specify which will be used, that is determined by the @Named("LowPricingStrategyA") annotation in MyClass. Now imagine the 'pricing strategy group' has 5 pairs of classes A, B, C, D & E...a High and a Low for each.
What I'm wondering is how to get the control of this centralized instead of in 5 different classes.
-Dave