遇到多个构造器参数采用构建器 《Effective Java》读书笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq951127336/article/details/79966800

常见构造器写法有三种,重叠构造器,JavaBean,Builder模式。本文记录下三种方式的优劣

1.重叠构造器

优点:脑子里最先想到,最简单。
缺点:代码量大,可读性低,且参数如果输入错误,可能该错误不会报出。

代码

public class NutritionFacts{
    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 NutritionFacts(int servingSize,int servings){
        this(servingSize,servings,0);
    }
    public NutritionFacts(int servingSize,int servings,int calories){
        this(servingSize,servings,calories,0);
    }
    public NutritionFacts(int servingSize,int servings,int calories,int fat){
        this(servingSize,servings,calories,fat,0);
    }
    public NutritionFacts(int servingSize,int servings,int calories,int fat,int sodium){
        this(servingSize,servings,calories,fat,sodium,0);
    }
    public NutritionFacts(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;
    }
}

2.JavaBean模式

优点:代码可读性搞,代码量较少
缺点:线性不安全

代码

public class NutritionFacts{
    private int servingSize;
    private int servings;
    private int calories;
    private int fat;
    private int sodium;
    private int carbohydrate;
    public NutritionFacts(){};
    public void setServingSize(int var){this.servingSize = var;}
    public void setServings(int var){this.servings = var;}
    public void setCalories(int var){this.calories = var;}
    public void setFat(int var){this.fat = var;}
    public void setSodium(int var){this.sodium = var;}
    public void setCarbohydrate(int var){this.carbohydrate = var;}
}

3.Builder模式

优势:可读性高,利于开发,代码量较少,且线性安全
缺点:增加性能开销

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

        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

        public Builder(int servingSize ,int servings){
            this.servings = servings;
            this.servingSize = servingSize;
        }
        public Builder calories(int val){
            calories = val;
            return this;
        }
        public Builder fat(int val){
            fat = val;
            return this;
        }
        public Builder sodium(int val){
            sodium = val;
            return this;
        }
        public Builder carbohydrate(int val){
            carbohydrate = val;
            return this;
        }
        public NutritionFacts build(){
            return new NutritionFacts(this);
        }
        private NutritionFacts(Builder builder){
            servingSize = builder.servingSize;
            servings = builder.servings;
            calories = builder.calories;
            fat = builder.fat;
            sodium = builder.sodium;
            carbohydrate = builder.carbohydrate;
        }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页