多构造器下考虑选用Builder
2016-07-14 15:49
211 查看
多参数,多构造器下考虑。
易读
线程安全
简单例子
易读
线程安全
简单例子
package com.wj.test.cases.model; import java.sql.Timestamp; import java.util.Date; /** * Created by wangjia on 16/3/24 22:15 */ public class User { private final String name; private final int age; private final Date birthday; private final Timestamp createTime; private final Timestamp updateTime; private User(Builder builder) { this.name = builder.name; this.age = builder.age; this.createTime = builder.createTime; this.birthday = builder.birthday; this.updateTime = builder.updateTime; } public static class Builder { private final String name; private int age; private Timestamp createTime; private Date birthday; private Timestamp updateTime; //required public Builder(String name) { this.name = name; } public Builder age(int age) { this.age = age; return this; } public Builder createTime(Timestamp createTime) { this.createTime = createTime; return this; } public Builder birthday(Date birthday) { this.birthday = birthday; return this; } public Builder updateTime(Timestamp updateTime) { this.updateTime = updateTime; return this; } public User build() { return new User(this); } } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", createTime=" + createTime + ", birthday=" + birthday + ", updateTime=" + updateTime + '}'; } public static void main(String[] args) { User heiheihei = new Builder("heiheihei").age(12).birthday(new Date()).build(); } }
相关文章推荐
- Web自动化框架LazyUI使用手册(7)--浏览器常用操作API
- Web自动化框架LazyUI使用手册(7)--浏览器常用操作API
- Arduino - 5V供电注意事项
- UESTC - 994 两个圆的公共面积
- NSOperationQueue 线程挂起,添加依赖
- iOS开发UI篇—UIWindow简单介绍
- KendoUI模板引擎 - 结合组件使用
- DOM对象与query对象说明及相互转换
- build.gradle
- TrueCrypt代码 之 几个文件操作函数,经常会用到
- Web自动化框架LazyUI使用手册(6)--8种控件对应的class,及可对其进行的操作
- Web自动化框架LazyUI使用手册(6)--8种控件对应的class,及可对其进行的操作
- Labelling Unsegmented Sequence Data with Recurrent Neural Networks(笔记)
- 【报错】"The constructor Notification(int, CharSequence, long) is deprecated
- AbstractQueuedSynchronizer(十)——应用之ReentrantLock
- jira和confluence配置问题
- No Suitable Driver Found For Jdbc的解决方法
- TrueCrypt代码 之 编码转换
- HDU 5493 Queue 线段树
- easyui combobox