Java类在多可选参数的情况,构造器的写法

本篇内容来自于个人在学习《effective java》一书时的理解,记录下自己的学习情况

1. 重叠构造器

public class NutritionFactsConstructor {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    public NutritionFactsConstructor(int servingSize, int servings){
        this(servingSize, servings, 0);
    }
    public NutritionFactsConstructor(int servingSize, int servings, int calories){
        this(servingSize, servings, calories, 0);
    }
    public NutritionFactsConstructor(int servingSize, int servings, int calories, int fat){
        this(servingSize, servings, calories, fat, 0);
    }
    public NutritionFactsConstructor(int servingSize, int servings, int calories, int fat, int sodium){
        this(servingSize, servings, calories, fat, sodium, 0);
    }
    public NutritionFactsConstructor(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate){
        this.servingSize = servingSize;
        this.servings = servings;
        this.calories = calories;
        this.fat = fat;
        this.sodium = sodium;
        this.carbohydrate = carbohydrate;
    }
}

缺点: 在使用时,当选择在参数列表中位置靠后的可选参数时,必须同时给必选参数和选择的最后一个可选参数之间的无关参数赋值。

测试:

NutritionFactsConstructor n1 = new NutritionFactsConstructor(1, 1);
NutritionFactsConstructor n2 = new NutritionFactsConstructor(1, 2, 6);
NutritionFactsConstructor n3 = new NutritionFactsConstructor(1, 3, 0, 5);
System.out.println("n1 = " + n1 + "\nn2 = " + n2 + "\nn3 = " + n3);
------------------------------------------------------------------------------------------------
输出:
n1 = NutritionFactsConstructor{servingSize=1, servings=1, calories=0, fat=0, sodium=0, carbohydrate=0}
n2 = NutritionFactsConstructor{servingSize=1, servings=2, calories=6, fat=0, sodium=0, carbohydrate=0}
n3 = NutritionFactsConstructor{servingSize=1, servings=3, calories=0, fat=5, sodium=0, carbohydrate=0}

2. JavaBeans

public class NutritionFactsJavaBeans {
    private int servingSize = -1;
    private int servings = -1;
    private int calories = 0;
    private int fat = 0;
    private int sodium = 0;
    private int carbohydrate = 0;

    public NutritionFactsJavaBeans(int servingSize, int servings){
        this.servingSize = servingSize;
        this.servings = servings;
    }

    @Override
    public String toString() {
        return "NutritionFactsJavaBeans{" +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calories=" + calories +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }

    public int getServingSize() {
        return servingSize;
    }

    public void setServingSize(int servingSize) {
        this.servingSize = servingSize;
    }

    public int getServings() {
        return servings;
    }

    public void setServings(int servings) {
        this.servings = servings;
    }

    public int getCalories() {
        return calories;
    }

    public void setCalories(int calories) {
        this.calories = calories;
    }

    public int getFat() {
        return fat;
    }

    public void setFat(int fat) {
        this.fat = fat;
    }

    public int getSodium() {
        return sodium;
    }

    public void setSodium(int sodium) {
        this.sodium = sodium;
    }

    public int getCarbohydrate() {
        return carbohydrate;
    }

    public void setCarbohydrate(int carbohydrate) {
        this.carbohydrate = carbohydrate;
    }
}

特点:通过构造器给必选参数赋值,然后使用setter方法来给需要的可选参数赋值。
缺点:该种方法无法实现类的不可变性。
测试:

NutritionFactsJavaBeans n1 = new NutritionFactsJavaBeans(1, 1);
n1.setFat(1);
n1.setCarbohydrate(5);
System.out.println(n1);
------------------------------------------------------------------------------------------------
输出:
NutritionFactsJavaBeans{servingSize=1, servings=1, calories=0, fat=1, sodium=0, carbohydrate=5}

3. 构建器

public class NutritionFactsBuilder {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    private NutritionFactsBuilder(Builder builder){
        this.servingSize = builder.servingSize;
        this.servings = builder.servings;
        this.calories = builder.calories;
        this.fat = builder.fat;
        this.sodium = builder.sodium;
        this.carbohydrate = builder.carbohydrate;
    }

    @Override
    public String toString() {
        return "NutritionFactsBuilder{" +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calories=" + calories +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }

    public static class Builder{
        private final int servingSize;
        private final int servings;
        private int calories;
        private int fat;
        private int sodium;
        private int carbohydrate;

        public Builder(int servingSize, int servings){
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder calories(int value){
            this.calories = value;
            return this;
        }
        public Builder fat(int value){
            this.fat = value;
            return this;
        }
        public Builder sodium(int value){
            this.sodium = value;
            return this;
        }
        public Builder carbohydrate(int value){
            this.carbohydrate = value;
            return this;
        }

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

特点:先实例化静态内部类的对象,设定好参数后再通过build方法调用外部类的构造器从而返回外部类的对象。在使用方法上具有非常好的可读性。
缺点:在非常追求性能优化时,静态内部类可能会影响性能。

测试:

NutritionFactsBuilder n1 = new NutritionFactsBuilder.Builder(1, 1).calories(1).carbohydrate(1).build();
NutritionFactsBuilder n2 = new NutritionFactsBuilder.Builder(2,2).fat(1).sodium(1).build();
System.out.println(n1);
System.out.println(n2);
------------------------------------------------------------------------------------------------
输出:
NutritionFactsBuilder{servingSize=1, servings=1, calories=1, fat=0, sodium=0, carbohydrate=1}
NutritionFactsBuilder{servingSize=2, servings=2, calories=0, fat=1, sodium=1, carbohydrate=0}

你可能感兴趣的:(Java类在多可选参数的情况,构造器的写法)