首页 > 其他 > 详细

Effective Java 02 - Consider a builder when faced with many constructor parameters

时间:2014-02-25 08:13:07      阅读:253      评论:0      收藏:0      [点我收藏+]

Summary

The Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful(>4) of parameters.

   

Advantage

  1. It simulates named optional parameters which is easily used to client API.
  2. Detect the invariant failure(validation error of field) as soon as the invalid parameters are passed, instead of waiting for build to be invoked.
  3. The builder can fill in some fields automatically such as a serial number.
  4. Avoid Class.newInstancebreaks compile-time exception checking

Disadvantage

  1. A builder must be created first. It‘s time cost and verbose. It would be better to be used when there are more than 4 parameters.

       

   

Demo

package com.effectivejava.creatingobject;

   

/**

* @author Kaibo

*

*/

// Builder Pattern

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 static class Builder {

// 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 carbohydrate = 0;

private int sodium = 0;

   

public Builder(int servingSize, int servings) {

this.servingSize = servingSize;

this.servings = servings;

}

   

public Builder calories(int val) {

calories = val;

return this;

}

   

public Builder fat(int val) {

fat = val;

return this;

}

   

public Builder carbohydrate(int val) {

carbohydrate = val;

return this;

}

   

public Builder sodium(int val) {

sodium = 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;

}

   

/*

* (non-Javadoc)

*

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

// TODO Auto-generated method stub

return String

.format("NutritionFacts[servingSize:%d, servings:%d, calories: %d, fat: %d, sodium: %d, carbohydrate: %d]",

this.servingSize, this.servings, this.calories,

this.fat, this.sodium, this.carbohydrate);

}

   

}

   

   

   

NutritionFacts nutritionFacts = new NutritionFacts.Builder(10, 100)

.calories(3).carbohydrate(2).fat(1).sodium(4).build();

System.out.println(nutritionFacts.toString());

   

/*******************************************/

// A builder for objects of type T

public interface Builder<T> {

public T build();

}

   

Tree buildTree(Builder<? extends Node> nodeBuilder) { ... }

   

   

   

   

Effective Java 02 - Consider a builder when faced with many constructor parameters

原文:http://www.cnblogs.com/haokaibo/p/3564143.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!