Lombok and Builder Design Pattern

Athul RAVINDRAN
2 min readJan 23, 2020

--

@Builder — I consider this as the most useful annotation and if combined with @Data, it becomes a deadly combo for creating POJOs.

Using this annotation, Lombok creates modifies the POJO by adding a static subclass with Builder design pattern.

What is builder pattern and when is it used ?? A builder pattern is used for constructing complex objects without needing to change the structure of it.

For example lets take Employee object, An employee object could have mandatory fields like employeeId, firstName, lastName; department and so on and can also have optional attributes as well.

You do not want the state of employee object to change after it is created and hence it is recommended to have an immutable employee class. To achieve that, all the members of the class has to be final.

public class EmployeeVanilla {

private final String firstName; //mandatory
private final String lastName; //mandatory
private final String employeeId; // mandatory
private final String optionalField1; //optional
private final String optionalField2; //optional
private final String optionalField3; //optional

Now to initialize the members, the class requires an allArgsConstructor. Imagine you want to create an employee object with some mandatory and optional attributes, for every such combination, you will need a constructor.

To make it worst, now you have to add one more mandatory attribute called “dateOfJoining”. Every single constructor has to be changed to accomdate the change. This is where builder pattern comes to the rescue.

public class EmployeeVanilla {


private final String firstName; //mandatory
private final String lastName; //mandatory
private final String employeeId; // mandatory
private final String optionalField1; //optional
private final String optionalField2; //optional
private final String optionalField3; //optional

public EmployeeVanilla (EmployeeVanillaBuilder employeeVanillaBuilder)
{
this.employeeId = employeeVanillaBuilder.employeeId;
this.firstName = employeeVanillaBuilder.firstName;
this.lastName = employeeVanillaBuilder.lastName;
this.optionalField1 = employeeVanillaBuilder.optionalField1;
this.optionalField2 = employeeVanillaBuilder.optionalField2;
this.optionalField3 = employeeVanillaBuilder.optionalField3;
}

public static class EmployeeVanillaBuilder
{
private final String firstName; //mandatory
private final String lastName; //mandatory
private final String employeeId; // mandatory
private String optionalField1; //optional
private String optionalField2; //optional
private String optionalField3; //optional

public EmployeeVanillaBuilder (String firstName, String lastName, String employeeId)
{
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}

public EmployeeVanillaBuilder optionalField1 (String optionalField1)
{
this.optionalField1 = optionalField1;
return this;
}

public EmployeeVanillaBuilder optionalField2 (String optionalField2)
{
this.optionalField2 = optionalField2;
return this;
}

public EmployeeVanillaBuilder optionalField3 (String optionalField1)
{
this.optionalField3 = optionalField3;
return this;
}

public EmployeeVanilla build()
{
EmployeeVanilla employeeVanilla = new EmployeeVanilla(this);
return employeeVanilla;
}
}
}

Now the power of Lombok…This whole messy big POJO can become like below

That’s it !!!!

--

--