Builder creational design pattern with Lombok
3 min readMar 10, 2024
The basics of lombok annotations
package com.basic.example;
import lombok.*;
@Getter
@Setter
/*
Use @Data which Combines @Getter, @Setter, @ToString, @EqualsAndHashCode, and @NoArgsConstructor
for all fields in the class.
*/
// "runtime", "director" use comma separated list to exclude multiple values.
@EqualsAndHashCode(exclude = {"runtime"}) // Exclude the 'runtime' field from equality check.
@ToString
/*
@ToString annotation in Lombok hides the actual implementation of the toString() method.
You won't see the generated code in your Java source file.
@Override
public String toString() {
return "com.basic.example.Movies(name=" + name + ", year=" + year + ", runtime=" + runtime + ")";
*/
public class Movies {
String name;
int year;
double runtime;
/*
@NonNull
Exception in thread "main" java.lang.NullPointerException: ID is marked non-null but is null
This will throw above exception, if we set this value as null , movies2.setID(null);
*/
@NonNull
private String ID;
}
@NonNull:
Ensures that the field cannot be set to null.
If you try to instantiate ID with a null value, it will throw a NullPointerException at runtime.
We can preview the code that generated from Lombok by right click on Movies class Refactor > Delombok and select the method that wish to preview. Select All lombok annotation value to see for all.
In main class
package com.basic.example;
public class Home {
public static void main(String[] args) {
Movies movies1 = new Movies();
movies1.setName("Titanic");
movies1.setYear(1996);
movies1.setRuntime(195.5); // Runtime in minutes theatrical version
movies1.setID("PARA-01");
Movies movies2 = new Movies();
movies2.setName("Titanic");
movies2.setYear(1996);
movies2.setRuntime(345.12); // Runtime in minutes Director cuts version
movies1.setID("PARA-02");
System.out.println(movies1.equals(movies2)); // Output: true
// Using the generated toString() method
System.out.println(movies1.toString());
}
}
Output-
false
Movies(name=Titanic, year=1996, runtime=195.5, ID=PARA-02)
Builder creational design pattern with Lombok
package com.builder.example;
import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
@Data
@Builder
public class Person {
private String name;
private int age;
private String address;
@NonNull // Here I marked ID as a not null field so it must be defined can't be null or empty
private String ID;
/*
We annotate the Person class with @Builder.
Lombok generates a builder class named PersonBuilder within the Person class,
which provides methods for setting each field individually.
*/
}
Main class
package com.builder.example;
public class MainBuilderPerson {
public static void main(String[] args) {
Person person=Person.builder().name("John doe").age(44).address("PO BOX 666,Kansas").ID("V-666").build();
Person female=Person.builder().name("Jane doe").age(23).ID("V-666").build();
System.out.println(person);
System.out.println(female);
/*
In the main() method, we use the generated builder to construct a Person object by chaining method calls to set the desired fields.
The build() method finalizes the construction of the Person object.
This allows for a concise and readable way to construct objects, especially when dealing with classes that have many optional parameters.
with the builder pattern generated by Lombok's @Builder, you can specify values only for the parameters you want to set, leaving the rest with their default values or null if no default values are provided.
*/
System.out.println(person.equals(female)); // Output: false
}
}
Output
Person(name=John doe, age=44, address=PO BOX 666,Kansas, ID=V-666)
Person(name=Jane doe, age=23, address=null, ID=V-666)
false
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>LombokJavaDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>LATEST</version> <scope>provided</scope>
</dependency>
</dependencies>
</project>