I'm Java beginner and IoC as well. How to do stuff:
public class Foo{
//private Bar bar; //Bar is an interface
private int var;
public Foo(){
}
public void setVar(int var){
this.var = var;
}
public Bar getBar(){
if(var==1){
return new BarImpl1(); //an implemantation of Bar interface
}
else if(var==2){
return new BarImpl2(); //an implemantation of Bar interface
}
else{
return new BarImpl(); //an implemantation of Bar interface
}
}
}
in IoC way on Guice example?
public class Foo{
private Bar bar; //Bar is an interface
private int var;
@Inject
public Foo(Bar bar){
this.bar = bar;
}
public void setVar(int var){
this.var = var;
}
public Bar getBar(){
return bar; // or what else??
}
}
How should I configure my injector?
@Override
protected void configure() {
bind(Bar.class).to(BarImpl.class);
//and what else??
}