Inside a module, if I need to provide a different implementation of an interface based on a variable known at module construction time I can put the logic inside the @Provid
A possible solution would be using @Named("foo")
annotation in conjunction with favoring component provision method over manual injection, which would however mean that your state would be independent from the module itself, and you'd be the one to make your choice
@Component(modules={FooModule.class})
public interface AppComponent {
@Named("STATE_1")
FooInterface fooImpl1();
@Named("STATE_2")
FooInterface fooImpl2();
...
@Named("STATE_10")
FooInterface fooImpl10();
}
@Module
public FooImpl1Module {
@Provides
@Named("STATE_1")
FooInterface provideFooImpl1(Context context) {
return new FooImpl1(context);
}
@Provides
@Named("STATE_2")
FooInterface provideFooImpl2(Context context) {
return new FooImpl2(context);
}
...
@Provides
@Named("STATE_10")
FooInterface provideFooImpl10(Context context) {
return new FooImpl10(context);
}
}
Then you can call
FooInterface fooInterface = component.fooImpl1();