博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Effective Java 02 Consider a builder when faced with many constructor parameters
阅读量:4697 次
发布时间:2019-06-09

本文共 2668 字,大约阅读时间需要 8 分钟。

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;

// 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) { ... }

 

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. 

转载于:https://www.cnblogs.com/haokaibo/p/consider-a-builder-when-faced-with-many-constructor-parameters.html

你可能感兴趣的文章
iOS10 UI教程视图和子视图的可见性
查看>>
Maven学习笔记
查看>>
FindChildControl与FindComponent
查看>>
1、简述在java网络编程中,服务端程序与客户端程序的具体开发步骤?
查看>>
C# Web版报表
查看>>
中国城市json
查看>>
android下载手动下载Android SDK
查看>>
C++学习:任意合法状态下汉诺塔的移动(原创)
查看>>
学霸修炼的秘籍
查看>>
Duplicate 复制数据库 搭建Dataguard
查看>>
leetcode133 - Clone Graph - medium
查看>>
Mybatis(一)入门
查看>>
DDR工作原理(转)
查看>>
(Frontend Newbie) Web三要素(一)
查看>>
(转载-学习)python wsgi 简介
查看>>
QPushButton 控制两种状态
查看>>
一点小基础
查看>>
PHP 自动加载类 __autoload() 方法
查看>>
JDK中的Timer和TimerTask详解(zhuan)
查看>>
【python练习】ATM&购物商城程序
查看>>