码迷,mamicode.com
首页 > 编程语言 > 详细

Effective Java 英文 第二版 读书笔记 Item 2:Consider a builder when faced with many constructor parameters.

时间:2015-09-03 11:30:13      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

 

package builderManyPara;

//JavaBeans Pattern - allows inconsistency,mandates mutability 
public class NutritionFactsBean {
    // Parameters initialized to default values(if any)
    // required
    private int servingSize = -1;
    private int servings = -1;

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

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

    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;
    }

    public static void main(String[] args) {
        NutritionFactsBean nutritionFactsBean=new NutritionFactsBean(240, 8);
        nutritionFactsBean.setFat(8);
    }
}

A JavaBean may be in an inconsistent state partway through its construction.

The JavaBean pattern precludes the possibility of making a class immutable.

 

package builderManyPara;

//Builder Pattern
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;

    public static class Builder implements Builders<NutritionFactsBuilder> {
        // Required parameters
        private final int servingSize;
        private final int servings;

        // Optional parameters - initialized to default values
        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

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

        public Builder calories(int calories) {
            this.calories = calories;
            return this;
        }

        public Builder fat(int fat) {
            this.fat = fat;
            return this;
        }

        public Builder sodium(int sodium) {
            this.sodium = sodium;
            return this;
        }

        public Builder carbohydrate(int carbohydrate) {
            this.carbohydrate = carbohydrate;
            return this;
        }

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

    }

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

    public static void main(String[] args) {
        NutritionFactsBuilder nutritionFacts = new NutritionFactsBuilder.Builder(240, 8)
                .calories(100).sodium(35).build();
    }
}

 

package builderManyPara;

//A builder for objects of type
public interface Builders<T> {
    public T build();
}

 

In summary,the Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters,especially if most of those parameters are optional.Client code is much easier to read and write with builders than with the traditional telescoping constructor pattern,and builders are much safer than JavaBeans.

 

Effective Java 英文 第二版 读书笔记 Item 2:Consider a builder when faced with many constructor parameters.

标签:

原文地址:http://www.cnblogs.com/linkarl/p/4779955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!