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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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)实现单例模式. ...

  4. 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 ...

  5. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  6. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  7. Effective Java —— 多字段下考虑使用建造者模式构建实例

    本文参考 本篇文章参考自<Effective Java>第三版第二条"Consider a builder when faced with many constructor pa ...

  8. 《Effective Java》读书笔记 - 2.创建和销毁对象

    Chapter 2 Creating and Destroying Objects item 1:Consider static factory methods instead of construc ...

  9. Java之进阶(1) -《Effective Java》

    第1章 引言 第2章 创建和销毁对象 第1条:考虑用静态工厂方法代替构造器(Consider static factory methods instead of constructors) 第2条:遇 ...

随机推荐

  1. BZOJ3394: [Usaco2009 Jan]Best Spot 最佳牧场

    3394: [Usaco2009 Jan]Best Spot 最佳牧场 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 11  Solved: 9[Sub ...

  2. BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

    3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved:  ...

  3. 让PV10000+的秘诀

    原文地址:http://www.phonegap100.com/article-410-1.html 让PV10000+的秘诀 2015-5-4 21:49| 发布者: admin| 查看: 122| ...

  4. flume-agent实例

    flume    多种适配,多样化的数据收集    核心概念        event:一条消息        client:访问者        agent:            重要组件Sour ...

  5. LeetCode 6. ZigZag Conversion Question

    题意:给你一个字符串和行数numRows,要求把该字符串变成一个"之"字形状后,按行数输出该字符串. 例子:"ABCDEFGHIJKLMNO", 4. 该字符串 ...

  6. java foreach记录

    实现原理解释: http://blog.csdn.net/a596620989/article/details/6930479 http://stackoverflow.com/questions/8 ...

  7. SQL VS NoSQL

    (关系型与非关系型)数据库的区别: 关系型和非关系型数据库的主要差异是数据存储的方式 1.1 数据表 VS 数据集 关系型数据天然就是表格式的,因此存储在数据表的行和列中.数据表可以彼此关联协作存储, ...

  8. 使用memcached加速web应用实例

    在实际应用中,一般会把数据库查询的结果保存到memcached中,下次訪问数据库时直接从memcached中获取.而不再进行数据库操作,这样非常大的程度上减轻了数据库的负担. [演示样例]: < ...

  9. Selenium2(webdriver)入门之TestNG的使用

    一.在Eclipse中安装TestNG 1.打开eclipse-->help-->Install New Software-->Add,输入Name和Location后,点击OK. ...

  10. 1034 - Navigation

    Global Positioning System (GPS) is a navigation system based on a set of satellites orbiting approxi ...