I've been using guice for a project.
I have an abstract class which has many implementations. To use the right implementation I use a factory that receives a parameter and then returns the right instance.
Demo Code
@Singleton
public class MyFactory {
private final Foo foo;
@Inject
public MyFactory(final Foo foo) {
this.foo = foo;
}
public A create(final Bar bar) {
switch (bar) {
case 0:
return new B(foo, bar);
case 1:
return new C(foo, bar);
//this goes on
}
}
}
public abstract A {
public A(final Bar bar) {
//do sth
}
}
public B extends A {
private final Foo foo;
public B(final Foo foo, final Bar bar) {
super(bar);
this.foo = foo;
}
}
public C extends A {
private final Foo foo;
public C(final Foo foo, final Bar bar) {
super(bar);
this.foo = foo;
}
}
What I want to know, if I can replace the factory with Guice
to inject directly the implementations of A
(note that they should use assisted injection)?
Thanks.