构建器builder模式+lombok@Builder的介绍及使用

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

构建器builder模式+lombok@Builder的介绍及使⽤
注意⚠ :
带有@Builder的类,如果加了@NoArgsConstructor,那么也必须加@AllArgsConstructor,因为@Builder需要全参数的构造函数。

1、重叠构造函数的优化
对Effective Java书中第⼆章第⼆条遇到多个构造器参数时要考虑使⽤构建器进⾏复盘。

假设某个类,现在有3个必选属性,有5个可选属性.(为了代码简洁,后⾯都只写⼀个必选属性,2个可选属性.懂就⾏).
那么现在想提供完善的创建该类的机制,该怎么办呢?
1、⾸先是⽅法1-使⽤重叠的构造⽅法.
这是⼤家都熟悉的⽅法,重载很多个构造⽅法,每个的参数都不⼀样,总有⼀款适合您!
public class Student {
// 必选
String name;
// 可选
int age;
String title;
public Student(String name) {
= name;
}
public Student(String name, int age) {
= name;
this.age = age;
}
public Student(String name, int age, String title) {
= name;
this.age = age;
this.title = title;
}
}
三个构造⽅法是不是已经脑壳疼了,注意,在真正的代码中,30个属性的类也不少见噢,写死⼈了要.
⽽且这样还有⼀个缺点,可读性太差了,在写的时候还好⼀些,在调⽤的时候你会看到编译器提醒你有30个构造⽅法可以调⽤,并且只显⽰参数类型不显⽰参数名字(⽐如⼀个8个int参数的构造⽅法,⿁知道应该按照什么顺序传⼊啊),你根本不知道该怎么⽤…
2、那么还有第⼆种⽅法:javabean,即使⽤setter.
对每个属性都提供set⽅法,这样客户端可以队医的调⽤set⽅法来传⼊他们想要的参数.
private int age;
private String title;
public void setName(String name) {
= name;
}
public void setAge(int age) {
this.age = age;
}
public void setTitle(String title) {
this.title = title;
}
}
调⽤代码:
Student student = new Student();
student.setName("huyan");
student.setAge(1);
student.setTitle("666");
这样⼦的好处是可读性好,但是不好的地⽅是不安全,你根本不知道客户端会以什么奇怪的⽅式使⽤你的类.
3、可以使⽤Builder模式.
private int age;
private String title;
private static class Builder {
// 必选
private String name;
// 可选
private int age;
private String title;
public Builder(String name) {
= name;
}
public Builder age(int age) {
this.age = age;
return this;
}
public Builder title(String s) {
this.title = s;
return this;
}
public Student build() {
return new Student(this);
}
}
private Student(Builder builder) {
name = ;
age = builder.age;
title = builder.title;
}
这⾥⾯有⼏个重要的点:
将Student类的构造⽅法私有化,所以想要新建Student必须使⽤Builder.
Builder只有⼀个构造⽅法,传⼊必选的参数,这样可以保证每个Student都会有必选参数.
对所有的可选参数提供同名⽅法,使得可选参数可以被设置,同时返回⾃⾝.
Builder提供build⽅法,调⽤Student私有的构造⽅法,返回对象.
客户端的调⽤⽅法如下:
public static void main(String[] args) {
Student s = new Builder("huyan").age(11).title("888").build();
}
使⽤Builder模式实现了上⾯其他两种⽅式的优点:安全且可读性⾼.
限制了参数,保证必选参数肯定有.
可读性好,传⼊每个可选参数单独调⽤⽅法,可以明确的知道每个参数的意义.
链式调⽤看起来好看.
当然,builder模式也有缺点:
在创建的过程中多创建了⼀个对象,这对性能肯定是有影响的,所以在极限要求性能的场景可以注意⼀下.
代码⽐重叠构造器的代码都多…写起来也挺累啊.
等等,⽼是写Builder类?lombok了解⼀下?只需要⼀个注解就可以实现上⾯这样⼦的效果噢~.所以接下来学习⼀下,lombok是都有哪些使⽤⽅式.
2、lombok
lombok是⼀套⼩⼯具,可以帮助你减少样板式或者实现⼀些别的功能.
lombok的作⽤仅在源码起作⽤,也就是说,lombok会帮你在编译的过程中添加⼀些东西,使得你不⽤⾃⼰写,⽽⼀旦⽣成了class⽂件,lombok的作⽤就已经结束.
Builder:⾸先看⼀下上⾯提到的Builder是怎么实现的.将Student类的代码清空,仅保留属性,然后在类名上加上@Builder注解:
@Builder
public class Student1 {
// 必选
private String name;
// 可选
private int age;
private String title;
}
然后使⽤IDEA进⾏编译,然后查看class⽂件,Idea会⾃动帮我们反编译拿到源⽂件.查看源代码,如下:
public class Student1 {
private String name;
private int age;
private String title;
@ConstructorProperties({"name", "age", "title"})
Student1(String name, int age, String title) {
= name;
this.age = age;
this.title = title;
}
public static Student1.Student1Builder builder() {
return new Student1.Student1Builder();
}
public static class Student1Builder {
private String name;
private int age;
private String title;
Student1Builder() {
}
public Student1.Student1Builder name(String name) {
= name;
return this;
}
public Student1.Student1Builder age(int age) {
this.age = age;
return this;
}
public Student1.Student1Builder title(String title) {
this.title = title;
return this;
}
public Student1 build() {
return new Student1(, this.age, this.title);
}
public String toString() {
return "Student1.Student1Builder(name=" + + ", age=" + this.age + ", title=" + this.title + ")";
}
}
}
可以看到,Builder的代码和上⾯我们⾃⼰写的⼀模⼀样,整个类的区别就是,Student1类提供了toBuilder()⽅法,返回⼀个Student1Builder,可以完全屏蔽⽤户对Builder类的感知.
下⾯列举⼀些常⽤的lombok的注解,并简要解释其作⽤.
@Getter/@Setter
可以应⽤在类上,属性上. ⾃动⽣成get/set⽅法.
@toString
⾃动⽣成toString⽅法.
@EqualsAndHashCode
⽣成equals和hashcode⽅法.
@RequiredArgsConstructor
⽣成⼀个必须参数的构造器.
@Data
快捷⽅法,相当于@Getter+ @Setter+ toString + EqualsAndHashCode + RequiredArgsConstructor.
@AllArgsConstructor 和 @NoArgsConstructor
⾃动⽣成全部参数和零个参数的构造⽅法.
@Log
包含⼀系列常⽤的log系统的注解,⽐如@Slf4j,@Log4j2等,⾃动⽣成⼀个全局final的logger供你使⽤.。

相关文档
最新文档