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. 最全的TV视频应用合集,包含50多款客户端,有丰富直播点播

    这是我目前找到的 最好的视频应用合集,与坛友分享下.有50多款视频客户端,基本覆盖目前市面上口碑比较好的视频应用了. 里面有丰富的直播客户端,像 龙龙直播.泰捷直播.果子 Tv.More Tv等,还有 ...

  2. 设计模式(十二): Flyweight享元模式 -- 结构型模式

    说明: 相对于其它模式,Flyweight模式在PHP实现似乎没有太大的意义,因为PHP的生命周期就在一个请求,请求执行完了,php占用的资源都被释放.我们只是为了学习而简单做了介绍. 1. 概述 面 ...

  3. C#的checked和unchecked

    C#的 checked关键字用于对整型算术运算和转换显式启用溢出检查. 简单点说,我们在进行数值计算时,运算结果可能会超出该类型能表达的数值范围,因而结果溢出.而这个溢出如果是含有变量的表达式的话,编 ...

  4. hdu 4746 Mophues

    莫比乌斯反演.先初始化出所有数有多少个质因子和mobius.然后处理mob_sum[ i ][ j ],表示当公因子的因子个数小于等于 j 个的mobius前 i 项和.然后分块求和即可. 分块处理部 ...

  5. java基础知识(二)

    java的布局管理: borderLayout:则将板块分为东西南北中五个方向,每添加一个组件就要指定组件摆放的方位,放置在东西南北四个方向的组件将贴边放置.当拉大Frame的时候,处在center( ...

  6. [C++]memcpy 小记

    #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { char a = ...

  7. iOS socket 实现tcp和服务器长链接的简单使用心得

    首先iOS端用了一个第三方的框架 GCDAsyncSocket 当然这个是CocoaAsyncSocket框架里面的一部分 Github下载地址https://github.com/robbiehan ...

  8. 添加链接服务器 SQL SERVER

    使用sql语句: exec sp_addlinkedserver @server='serverontest',@provider='sqloledb',@srvproduct='',@datasrc ...

  9. Bestcoder #47 B Senior&#39;s Gun

    Senior's Gun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  10. Linux安装WebLogic12

    # groupadd weblogic# useradd -g weblogic weblogic# passwd weblogic# mkdir -p /var/bea# chown -R webl ...