定义:将一个复杂对象的创建过程,封装到一个对象中(生成器),然后让客户端调用生成器按步骤构造对象。
代码:
public class Student {
// id
private Long id;
// 姓名
private String name;
// 性别
private Integer sex;
// 手机号码
private String mobile;
// 出生日期
private Date birthday;
// ----------提供默认构造函数和getter、setter方法
public Student() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
// ----------提供默认构造函数和getter、setter方法
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", mobile=" + mobile + ", birthday=" + birthday
+ "]";
}
public static Builder builder() {
return new Builder();
}
// 构造器
public static class Builder {
private Student student;
public Builder() {
this.student = new Student();
}
public Builder id(Long id) {
student.id = id;
return this;
}
public Builder name(String name) {
student.name = name;
return this;
}
public Builder sex(Integer sex) {
student.sex = sex;
return this;
}
public Builder mobile(String mobile) {
student.mobile = mobile;
return this;
}
public Builder birthday(Date birthday) {
student.birthday = birthday;
return this;
}
public Student build() {
return this.student;
}
}
}
测试:
public class Test {
public static void main(String[] args) {
// 学生1
Student student1 = Student.builder().id(1L).name("梅西").sex(1).mobile("13800138000").build();
// 打印学生1信息
System.out.println(student1);
// 学生2
Student student2 = Student.builder().id(2L).name("内马尔").sex(1).mobile("13800138002").build();
// 打印学生2信息
System.out.println(student2);
}
}
结果:
Student [id=1, name=梅西, sex=1, mobile=13800138000, birthday=null]
Student [id=2, name=内马尔, sex=1, mobile=13800138002, birthday=null]
注意:生成器模式还有其它写法,比如在调用Builder.build方法时才创建目标对象,然后将builder的属性复制过去,详情看《Effective Java 中文版 第2版》P11。
下面附上lombok(http://blog.didispace.com/java-lombok-1/)的Builder注解生成的生成器模式代码
1、源码
import java.util.Date;
import lombok.Builder;
// 省略getter/setter注解
@Builder
public class Student {
private Long id;
private String name;
private Date birthday;
}
2、反编译后的代码
import java.util.Date;
public class Student
{
private Long id;
private String name;
private Date birthday;
public static class StudentBuilder
{
private Long id;
private String name;
private Date birthday;
public String toString()
{
return "Student.StudentBuilder(id=" + this.id + ", name=" + this.name + ", birthday=" + this.birthday + ")";
}
public Student build()
{
return new Student(this.id, this.name, this.birthday);
}
public StudentBuilder birthday(Date birthday)
{
this.birthday = birthday;return this;
}
public StudentBuilder name(String name)
{
this.name = name;return this;
}
public StudentBuilder id(Long id)
{
this.id = id;return this;
}
}
public static StudentBuilder builder()
{
return new StudentBuilder();
}
Student(Long id, String name, Date birthday)
{
this.id = id;this.name = name;this.birthday = birthday;
}
}