我的代码看起来像 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.
我的问题是:
- 我应该在这里使用依赖注入还是超出了该技术的范围?
- 如果我应该在这里使用依赖注入,我如何使用 Google Guice 来完成它?