使用builder模式构建javabean

概述

使用private修饰构造器,使外部不能直接实例化Person对象.
通过静态工厂方法,获取一个Builder的实例.
通过Builder.build()方法发布Person对象.

代码演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

* 功能描述: 使用builder模式构建javabean
*
* @author sunzhiqiang
* @create 2018-08-12
*/
public class {

private String name;

private int age;

private String sex;

private (PersonBuilder builder) {
this.name = builder.name;
this.age = builder.age;
this.sex = builder.sex;
}

public static Person2.PersonBuilder getBuilder() {
return new PersonBuilder();
}

private static class PersonBuilder {

private String name;

private int age;

private String sex;

public PersonBuilder name(String name) {
this.name = name;
return this;
}

public PersonBuilder age(int age) {
this.age = age;
return this;
}

public PersonBuilder sex(String sex) {
this.sex = sex;
return this;
}

public Person2 build() {
return new Person2(this);
}
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getSex() {
return sex;
}


public String toString() {
return "Person2{" +
"name='" + name + ''' +
", age=" + age +
", sex='" + sex + ''' +
'}';
}

public static void main(String[] args) {

Person2 person2 = Person2.getBuilder()
.name("李四")
.age(10)
.sex("不确定")
.build();

System.out.println(person2);
}
}

当前类如果出现多个线程共享同一个builder,可能会导致线程不安全,而每一次调用工厂方法都new一个builder对象又有点浪费,下面我们使用ThreadLocal类来改写上面的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

* 功能描述: 使用builder模式构建javabean
*
* @author sunzhiqiang
* @create 2018-08-12
*/
public class {

private static final ThreadLocal<PersonBuilder> builder = new ThreadLocal<PersonBuilder>() {


protected PersonBuilder initialValue() {
return new PersonBuilder();
}
};

private String name;

private int age;

private String sex;

private (PersonBuilder builder) {
this.name = builder.name;
this.age = builder.age;
this.sex = builder.sex;
}

public static Person2.PersonBuilder getBuilder() {

return builder.get();
}

private static class PersonBuilder {

private String name;

private int age;

private String sex;

public PersonBuilder name(String name) {
this.name = name;
return this;
}

public PersonBuilder age(int age) {
this.age = age;
return this;
}

public PersonBuilder sex(String sex) {
this.sex = sex;
return this;
}

public Person2 build() {
return new Person2(this);
}
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getSex() {
return sex;
}


public String toString() {
return "Person2{" +
"name='" + name + ''' +
", age=" + age +
", sex='" + sex + ''' +
'}';
}

public static void main(String[] args) {

Person2 person2 = Person2.getBuilder()
.name("李四")
.age(10)
.sex("不确定")
.build();

System.out.println(person2);
}
}