持续补充中。。。
1. 什么是构建者
将复杂对象的构建和表示分离【用户不需要关注构建的复杂过程和细节】
2. Builder注解-最好的学习老师
核心思想:
1. 静态方法builder()。返回静态内部类
2. 根据需要使用链式编程赋值
3. build()返回当前类实例
@Builder
public class Dog {
private final Integer age;
private String name;
}
public class Dog {
private final Integer age;
private String name;
Dog(Integer age, String name) {
this.age = age;
this.name = name;
}
public static com.luoyu.aiyu.luo1.Dog.DogBuilder builder() {
return new com.luoyu.aiyu.luo1.Dog.DogBuilder();
}
public static class DogBuilder {
private Integer age;
public com.luoyu.aiyu.luo1.Dog.DogBuilder age(Integer age) {
this.age = age;
return this;
}
private String name;
public com.luoyu.aiyu.luo1.Dog.DogBuilder name(String name) {
this.name = name;
return this;
}
public com.luoyu.aiyu.luo1.Dog build() {
return new com.luoyu.aiyu.luo1.Dog(this.age, this.name);
}
public String toString() {
return "Dog.DogBuilder(age=" + this.age + ", name=" + this.name + ")";
}
}
}