0

我正在阅读 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构造函数参数

4

2 回答 2

1

...随着 House 构造函数的参数增加,使用构造函数管理它会很困难。

使用 Builder 模式(Effective Java item 2):

public class House {

    //these are immutable if you wish
    private final int mNumberOfWindows;
    private final int mNumberOfDoors;

    private House(HouseBuilder builder) {
        mNumberOfWindows = builder.mNumberOfWindows;
        mNumberOfDoors = builder.mNumberOfDoors;
    }

    public static class HouseBuilder {
        private int mNumberOfWindows; //set defaults here, make final and pass in builder constructor if they must be set
        private int mNumberOfDoors;

        public HouseBuilder windows(int numberOfWindows) {
            mNumberOfWindows = numberOfWindows;
            return this;
        }

        public HouseBuilder doors(int numberODoors) {
            mNumberOfDoors = numberODoors;
            return this;
        }

        public House build() {
            return new House(this);
        }
    }

    public int getNumberOfWindows() {
        return mNumberOfWindows;
    }

    public int getNumberOfDoors() {
        return mNumberOfDoors;
    }
}

用法:

    House.HouseBuilder hb = new House.HouseBuilder();
    hb.doors(4);
    hb.windows(3);
    House h = hb.build();

    House h2 = new House.HouseBuilder().doors(4)
            .windows(3).build();
于 2013-01-05T16:38:17.397 回答
0

I was do it in this way (sorry, it is a C#):

public interface IHouseConfiguration
{
    int noOfDoors { get; set; }
    int noOfWindows { get; set; }
    bool IsValid();
}

public class HouseConfiguration : IHouseConfiguration
{
    public HouseConfiguration(int noOfDoors, int noOfWindows)
    {
        this.noOfDoors = noOfDoors;
        this.noOfWindows = noOfWindows;
    }

    public int noOfDoors
    {
        get { return noOfDoors; }
        set { noOfDoors = value; }
    }

    public int noOfWindows
    {
        get { return noOfWindows; }
        set { noOfWindows = value; }
    }

    public bool IsValid()
    {
        //do validate calculus and return value. For a true, i do not sure it is IHouseConfiguration's duty...
        return true;
    }
}

public class Contractor
{
    public House buildHouse(IHouseConfiguration houseConfiguration)
    {
        if (houseConfiguration.IsValid())
        {
            return new House(houseConfiguration);
        }

        return null;
    }
}

public class House
{
    private Window[] windows;

    public House(IHouseConfiguration houseConfig) //Now, all incapsulated in IHouseConfiguration
    {
        Window[] windows = new Window[houseConfig.noOfDoors];
        this.windows = windows;
    }
}
于 2013-01-06T14:39:55.053 回答