Item2:Consider a builder when faced with many constructor parameters

当构造方法有多个参数时,可以考虑使用builder方式进行处理。

实例代码:

  1. public class NutritionFacts {
  2. private final int servingSize;
  3. private final int servings;
  4. private final int calories;
  5. private final int fat;
  6. private final int sodium;
  7. private final int carbohydrate;
  8.  
  9. public static class Builder{
  10. //以下两个参数是必须的
  11. private final int servingSize;
  12. private final int servings;
  13.  
  14. private int calories = 0;
  15. private int fat = 0;
  16. private int sodium = 0;
  17. private int carbohydrate= 0;
  18.  
  19. public Builder(int servingSize,int servings){
  20. this.servingSize = servingSize;
  21. this.servings = servings;
  22. }
  23.  
  24. public Builder calories(int val){
  25. calories = val;
  26. return this;
  27. }
  28.  
  29. public Builder fat(int val){
  30. fat = val;
  31. return this;
  32. }
  33.  
  34. public Builder sodium(int val){
  35. sodium = val;
  36. return this;
  37. }
  38.  
  39. public Builder carbohydrate(int val){
  40. carbohydrate = val;
  41. return this;
  42. }
  43.  
  44. public NutritionFacts build(){
  45. return new NutritionFacts(this);
  46. }
  47. }
  48.  
  49. private NutritionFacts(Builder builder){
  50. servingSize = builder.servingSize;
  51. servings = builder.servings;
  52. calories = builder.calories;
  53. fat = builder.fat;
  54. sodium = builder.sodium;
  55. carbohydrate = builder.carbohydrate;
  56. }
  57. }

使用方式:

  1. public class Test {
  2. public static void main(String[] args) {
  3. NutritionFacts nf = new NutritionFacts.Builder(200, 80)
  4. .calories(100).sodium(35).carbohydrate(20).build();
  5. }
  6. }

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. 两表关联更新,用于update 回滚

    create table test1 as select * from dba_objects; create table test2 as select * from dba_objects; cr ...

  2. COJ 0020 30201象棋中的皇后

    30201象棋中的皇后 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 在n×m的棋盘上放置两个相互攻击的皇后,总共有多少种不同 ...

  3. DPDK2.1开发者手册3-4

    环境抽象层EAL 环境抽象层的任务对访问底层资源例如硬件和内存提供入口.它提供了隐藏应用和库的特殊性性的通用接口.它的责任是初始化分配资源(内存,pci设备,定时器,控制台等等). EAL提供的典型服 ...

  4. 扩展Visual Studio IDE

    安装visual studio 2012 SDK 下载visual studio SDK. 安装可能遇到的问题 安装时报错:Visual Studio 2012 Install Fails: Prog ...

  5. IntelliJ IDEA创建web项目及异常问题解决

    IDEA配置Tomcat: 1.下载Tomcat,本次使用的是apache-tomcat-6.0.43 2.IDEA配置Tomcat 在idea中的Settings(Ctrl+Alt+s)(或者点击图 ...

  6. Java[1] Java学习书籍汇总(转)

    原文:http://www.cnblogs.com/hyl8218/p/5067000.html 学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: ...

  7. Css轮廓

    css code: p{ outline-width:2px; outline-color:aqua; outline-style: groove; }

  8. HTML的表格玩法

    HTML的表格玩法 HTML也是可已展示表格的,大体结构如下 <!DOCTYPE html> <html lang="en"> <head> & ...

  9. Highcharts 时间序列,可缩放的图表

    配置 图表 配置可缩放图表. chart.zoomType 指定了用户可以拖放的尺寸,用户可以通过拖动鼠标来放大,可能值是x,y或xy: var chart = { zoomType: 'x' }; ...

  10. 关于退运美国转基因玉米含有MRI 162转基因成分的质疑

    6月30日,新华社刊出文章"我国退运125.2万吨进口美国转基因玉米",读后有感. 文章说:国家质检总局办公厅副主任陆春明30日介绍,截至今年6月16日,全国出入境检验检疫机构共在 ...