zhaoyi escribió:
> Where do I put the creation logic when using BindingAnnotation or
> AssistedInject? The logic is:
>
> if(type == 1){
> return new Imple1();
> }else if(type ==2){
> return new Imple2();
> }
You can use annotations:
class Factory{
@Inject @IImple1 private final Interface1 imple1;
@Inject @IImple2 private final Interface1 imple2;
@Inject
Factory(String param1) {
this.param1 = param1;
}
public Interface1 getInterface(int type){
if(type == 1){
return imple1;
}else if(type ==2){
return imple2;
}
return null;
}
}
If you need some kind of lazy instantation you can use the help of a builder
in the imple* classes and call it from the factory.
For the @AssistedInject the docs pointed by Dhanji R.Prasana are very helpful.
public interface IFactory {
public FactoryHelper create(String param1);
}
public class FactoryHelper {
@Inject @IImple1 private final Interface1 imple1;
@Inject @IImple2 private final Interface1 imple2;
@Inject
Factory(@Assisted String param1) {
this.param1 = param1;
}
public Interface1 getInterface(int type){
if(type == 1){
return imple1;
}else if(type ==2){
return imple2;
}
return null;
}
}
public class FactoryModule implements Module {
public void configure(Binder binder) {
binder.bind(Interface1.class)
.annotatedWith(IIMple1.class)
.to(Imple1.class);
binder.bind(Interface1.class)
.annotatedWith(IImple2.class)
.to(Imple2.class);
}
}
@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD })
@Retention(RUNTIME)
@interface IImple1 {}
@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD })
@Retention(RUNTIME)
@interface IIMple2 {}
Bye
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---