1

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:

  1. Should I use dependency injection here or is this beyond the scope of the technique?
  2. If I should use dependency injection here, how can I accomplish it using Google Guice?

1 Answer 1

1

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 ...

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.