I have code that looks like the ff.:
Interface i;
if (someCondition) {
ImplementationA a = new ImplementationA();
a.setFieldA(someValueA);
a.setFieldB(someValueB);
i = a;
} else {
ImplementationB b = new ImplementationB();
b.setFieldC(someValueC);
i = b;
}
// Do something with i.
My questions are:
- Should I use dependency injection here or is this beyond the scope of the technique?
- If I should use dependency injection here, how can I accomplish it using Google Guice?
dynamic runtime injection is out of scope. you will have to configure which implementation to use via Modules. You could still use a factory (have a look at multibindings and assisted injection) and save yourself the work to set up your instances though …