I would like to eliminate too many of "new" creation in my code. So I decided to use builder pattern. I also would like to take advantage of Spring @Autowired if possible.
public class Car
{
@Autowired
private Radio radio;
@Autowired
private Speaker speaker;
@Autowired
private Engine engine;
private String model;
public Car createCar()
{
radio.add(speaker);
return this;
}
public static class Builder
{
private String model;
public Builder(){}
public Builder model(String model)
{
this.model = model;
return this;
}
}
}
I would like to use perhaps something like this.
Car car = new Car.Builder().model("A123").build();
Would this be possible to do?
Aucun commentaire:
Enregistrer un commentaire