我正在阅读 LoD 之后的可测试代码,但我脑子里一团糟。因此,请对这段代码提供任何指导。
public class HouseConfiguration {
private final int noOfDoors;
private final int noOfWindows;
//.... And so on
public HouseConfiguration(int noOfDoors, int noOfWindows){
this.noOfDoors = noOfDoors;
this.noOfWindows = noOfWindows;
//.....
}
//getters for config
}
public class Contractor {
public House buildHouse(HouseConfiguration houseConfiguration) throws Exception{
validateHouseConfiguration(houseConfiguration);
Window[] windows = new Window[houseConfiguration.getNoOfDoors()];
return new House(windows);
}
private void validateHouseConfiguration(HouseConfiguration houseConfiguration) {
//validation logic
}
}
public class House {
private Window[] windows;
//Now as the parameters increase it becomes a problem to pass so many arguments in constructor
public House(Window[] windows /*Also other arguments */) {
this.windows = windows;
}
}
现在随着House
构造函数的参数增加,使用构造函数管理它会很困难。许多依赖项将被通过。这是正确的做法吗?或者有没有更好的方法可以重构这段代码?
编辑:参考House
构造函数参数