Lombok is a Java library that greatly simplifies writing code by reducing boilerplate through the use of annotations. Among its many features, the @Builder
annotation is one of the most appreciated for its ability to use the pattern of the same name in a simple and intuitive way. This article will explore the potential of the builder pattern in detail, explaining how to use it and its advantages.
We have already seen in a previous article a simple example of how to use Lombok with Maven and IntelliJ so we will start from what has already been done.
The pattern builder
The Builder pattern is one of the most used creational design patterns in the world of object-oriented programming. Introduced by Gang of Four (GoF) in their book “Design Patterns: Elements of Reusable Object-Oriented Software”, this pattern solves problems related to the creation of complex objects, improving the readability and maintainability of the code.
This pattern separates the construction of a complex object from its representation, allowing you to create different representations of the same type of object. This is especially useful when a class has many optional attributes or when there are complex combinations of parameters.
The pattern builder allows you to solve several common programming problems:
- Constructors with many parameters: Avoid using constructors with many parameters, which can be difficult to read and maintain.
- Immutability: Facilitates the creation of immutable objects, where all fields are defined only once during the construction of the object.
- Conditional construction: Allows you to build objects flexibly, adding only the necessary fields based on certain conditions.
The @Builder annotation in Lombok
@Builder on the class
To use the pattern builder in Lombok simply write down the class you want to manage as in the following example:
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class RgbColor {
private int red;
private int green;
private int blue;
}
We used the same RgbColor class that we implemented in the previous tutorial but this time we annotated it with @Builder
instead of @AllArgsConstructor
.
At this point, to generate an instance you can use the pattern builder as in the following example:
public class Main {
public static void main (String[] args) {
RgbColor color = RgbColor.builder()
.red(30)
.green(22)
.blue(55)
.build();
System.out.println("red: " + color.getRed());
System.out.println("gren: " + color.getGreen());
System.out.println("blue: " + color.getBlue());
}
}
@Builder on methods
As an alternative to the previous example, you can define custom builders by applying the @Builder
annotation on a method.
In the following example we have defined a method to instantiate the RgbColor class by defaulting to the color blue:
import lombok.Builder;
import lombok.Getter;
@Getter
public class RgbColorBuilders {
@Builder(builderMethodName = "redGreenBuilder")
public static RgbColor createColor(int red, int green) {
return new RgbColor(red, green, 0);
}
}
Using the builder is similar to the previous example except that it will not be possible to change the value of the default property:
public class Main {
public static void main (String[] args) {
RgbColor color = RgbColorBuilders.redGreenBuilder()
.red(60)
.green(30)
.build();
System.out.println("red: " + color.getRed());
System.out.println("gren: " + color.getGreen());
System.out.println("blue: " + color.getBlue());
}
}
You can download the tutorial code from the following Git repository:
Lombok’s @Builder
annotation is a powerful tool for simplifying the creation of complex objects and improving the readability of your code. With its various customization and usage options, it becomes an indispensable resource for Java programmers who want to write clean and efficient code.