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条:遇 ...
随机推荐
- 两表关联更新,用于update 回滚
create table test1 as select * from dba_objects; create table test2 as select * from dba_objects; cr ...
- COJ 0020 30201象棋中的皇后
30201象棋中的皇后 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 在n×m的棋盘上放置两个相互攻击的皇后,总共有多少种不同 ...
- DPDK2.1开发者手册3-4
环境抽象层EAL 环境抽象层的任务对访问底层资源例如硬件和内存提供入口.它提供了隐藏应用和库的特殊性性的通用接口.它的责任是初始化分配资源(内存,pci设备,定时器,控制台等等). EAL提供的典型服 ...
- 扩展Visual Studio IDE
安装visual studio 2012 SDK 下载visual studio SDK. 安装可能遇到的问题 安装时报错:Visual Studio 2012 Install Fails: Prog ...
- IntelliJ IDEA创建web项目及异常问题解决
IDEA配置Tomcat: 1.下载Tomcat,本次使用的是apache-tomcat-6.0.43 2.IDEA配置Tomcat 在idea中的Settings(Ctrl+Alt+s)(或者点击图 ...
- Java[1] Java学习书籍汇总(转)
原文:http://www.cnblogs.com/hyl8218/p/5067000.html 学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: ...
- Css轮廓
css code: p{ outline-width:2px; outline-color:aqua; outline-style: groove; }
- HTML的表格玩法
HTML的表格玩法 HTML也是可已展示表格的,大体结构如下 <!DOCTYPE html> <html lang="en"> <head> & ...
- Highcharts 时间序列,可缩放的图表
配置 图表 配置可缩放图表. chart.zoomType 指定了用户可以拖放的尺寸,用户可以通过拖动鼠标来放大,可能值是x,y或xy: var chart = { zoomType: 'x' }; ...
- 关于退运美国转基因玉米含有MRI 162转基因成分的质疑
6月30日,新华社刊出文章"我国退运125.2万吨进口美国转基因玉米",读后有感. 文章说:国家质检总局办公厅副主任陆春明30日介绍,截至今年6月16日,全国出入境检验检疫机构共在 ...