Effective Java Item2:Consider a builder when faced with many constructor parameters
Item2:Consider a builder when faced with many constructor parameters
当构造方法有多个参数时,可以考虑使用builder方式进行处理。
实例代码:
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{
//以下两个参数是必须的
private final int servingSize;
private final int servings;
private int calories = 0;
private int fat = 0;
private int sodium = 0;
private int carbohydrate= 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 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;
}
}
使用方式:
public class Test {
public static void main(String[] args) {
NutritionFacts nf = new NutritionFacts.Builder(200, 80)
.calories(100).sodium(35).carbohydrate(20).build();
}
}
Effective Java Item2:Consider a builder when faced with many constructor parameters的更多相关文章
- Effective Java 02 Consider a builder when faced with many constructor parameters
Advantage It simulates named optional parameters which is easily used to client API. Detect the inva ...
- Effective Java 03 Enforce the singleton property with a private constructor or an enum type
Principle When implement the singleton pattern please decorate the INSTANCE field with "static ...
- Effective Java Item3:Enforce the singleton property with a private constructor or an enum type
Item3:Enforce the singleton property with a private constructor or an enum type 采用枚举类型(ENUM)实现单例模式. ...
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- Effective Java —— 多字段下考虑使用建造者模式构建实例
本文参考 本篇文章参考自<Effective Java>第三版第二条"Consider a builder when faced with many constructor pa ...
- 《Effective Java》读书笔记 - 2.创建和销毁对象
Chapter 2 Creating and Destroying Objects item 1:Consider static factory methods instead of construc ...
- Java之进阶(1) -《Effective Java》
第1章 引言 第2章 创建和销毁对象 第1条:考虑用静态工厂方法代替构造器(Consider static factory methods instead of constructors) 第2条:遇 ...
随机推荐
- LinkedList和ArrayList的区别
LinkedeList和ArrayList都实现了List接口,但是它们的工作原理却不一样.它们之间最主要的区别在于ArrayList是可改变大小的数组,而LinkedList是双向链接串列(doub ...
- YII增加全局函数
法1: 在使用Yii开发中我们经常会遇到一个问题,每次使用Yii的组件.扩展等,我们会像下面一样去写: <?php Yii::app()->user; Yii::app()->get ...
- 组合索引leaf 数据存储
1 Z 2 X 3 U 4 T 5 G 6 F 7 C 8 B 9 A 1 A 2 B 3 C 4 D Oracle的索引是以平衡树的方式组织存储的:保存的是索引列的值,以及该行的rowid的一部分( ...
- Java---设计模块(装饰模式)
★ 场景和问题 在不对原有对象类进行修改的基础上,如何给一个或多个已有的类对象提供增强额外的功能? ★ 引例 写一个MyBufferedReader类,使它能够对字符流(如FileReader.Inp ...
- 我学hash_map(1)
本文来源:http://blog.chinaunix.net/uid-26548237-id-3800125.html map是什么? map是键值对(key-value),复杂度是O(n).但是查 ...
- Redis应用场景 及其数据对象 string hash list set sortedset
原文地址:http://www.cnblogs.com/shanyou/archive/2012/09/04/2670972.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在 ...
- Razor 在JS中嵌入后台变量
HTML 中定义全局变量 @{ int CurrentUserId =ViewBag.CurrentUserId; } JS中取值方式var CurrentUserId = parseInt(@Htm ...
- zabbix discovery
preface(见面礼): 仅扫tcp端口: netstat -tnlp|egrep -i "$1"
- CURD
CURD是一个数据库技术中的缩写词,一般的项目开发的各种参数的基本功能都是CURD. 它代表创建(Create).更新(Update).读取(Read)和删除(Delete)操作. CURD 定义了用 ...
- MongoDB的数据类型
最近在写一个lua的MongoDB模块.MongoDB版本3.2,lua则是5.3.1.底层以C++来写,再把函数暴露给lua调用.但是在lua中打印结果时,发现了些奇怪的现象.首先,数据库中的内容: ...