Copyright belongs to the author, any form of reprint, please contact the author to obtain authorization and indicate the source.
Introduction to the
Lombok, as everyone knows, gives us a lot of convenience when using POJos, saving us a lot of time to write get, set, constructors, equal, and toString methods. In addition, Lombok facilitates the time Builder pattern through the @Builder annotation.
You just need to define a static public inner class. The following is a code example:
public class User {
private Integer id;
private String name;
private String address;
private User() {
}
private User(User origin) {
this.id = origin.id;
this.name = origin.name;
this.address = origin.address;
}
public static class Builder {
private User target;
public Builder() {
this.target = new User();
}
public Builder id(Integer id) {
target.id = id;
return this;
}
public Builder name(String name) {
target.name = name;
return this;
}
public Builder address(String address) {
target.address = address;
return this;
}
public User build() {
returnnew User(target); }}Copy the code
If lombok is used in your project, you can do so directly using the @Builder annotation
Modify the above class as follows:
import lombok.Builder;
import lombok.ToString;
/**
* @author wulongtao
*/
@ToString
@Builder
public class UserExample {
private Integer id;
private String name;
private String address;
}
Copy the code
How to Use:
UserExample userExample = UserExample.builder()
.id(1)
.name("aaa")
.address("bbb")
.build();
System.out.println(userExample);
Copy the code
Have a problem
In the process of using @Builder, I found a problem: the Builder object of the subclass has no properties of the parent class. This has caused some problems in use.
To this problem, the following solution is found
- For the parent class, use the @AllargsConstructor annotation
- For subclasses, write the full-parameter constructor manually, call the superclass’s full-parameter constructor internally, and use the @Builder annotation on the subclass’s full-parameter constructor
In this way, a subclass Builder object can use all the private properties of its parent class. But this solution has two side effects:
- Because the use of
@AllArgsConstructor
Note that the order in which a superclass constructor fields are declared is determined by the order in which they are declared. If a subclass constructor passes arguments in a different order and the field types are the same, it is difficult to find an error - If a superclass field is added or subtracted, the constructors of all subclasses are changed
Despite these two side effects, this solution is the only way I can find that subclasses using @Builder can use superclass attributes.
Lombok’s @Builder Annotation and Inheritance
How to add default values for fields in the mode using @Builder. Since the builder mode is used, the way that fields are normally declared in a class with default values is invalid and requires manipulation on the builder.
- Lombok will complete the existing Builder class and use the default values when you define the static inner class as the Builder and use the @Builder annotation
- The updated Lombok has the @Builder.Default declaration, annotated on fields that require Default values.
Side effects of this approach are also discussed in the comments section with the link: Using Lombok’s @Builder Annotation with Default Values