Normally, when we need to create an object with a lot of its information, we can use the Builder Pattern for that purpose.
For example, I have a Student class as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.huongdanjava.lombok; public class Student { private String firstName; private String middleName; private String lastName; private String country; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { return "Student{" + "firstName='" + firstName + '\'' + ", middleName='" + middleName + '\'' + ", lastName='" + lastName + '\'' + ", country='" + country + '\'' + '}'; } } |
To build this object with all information using the Builder Pattern, we need to create a Builder class as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.huongdanjava.lombok; public class StudentBuilder { private Student student; public StudentBuilder() { student = new Student(); } public StudentBuilder firstName(String firstName) { student.setFirstName(firstName); return this; } public StudentBuilder lastName(String lastName) { student.setLastName(lastName); return this; } public StudentBuilder middleName(String middleName) { student.setMiddleName(middleName); return this; } public StudentBuilder country(String country) { student.setCountry(country); return this; } public Student build() { return student; } } |
Then, we can use this StudentBuilder object to create a new Student object as below:
1 2 3 4 5 6 7 8 |
Student student = new StudentBuilder() .firstName("Khanh") .middleName("Huu") .lastName("Nguyen") .country("Viet Nam") .build(); System.out.printf(student.toString()); |
Result:
With Project Lombok, we have an easy way to create the Builder object by using @Builder annotation as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava.lombok; import lombok.*; @Getter @Setter @Builder @ToString public class Student { private String firstName; private String middleName; private String lastName; private String country; } |
Using @Builder annotation, when generating the class, a new Builder class will be added. With above example, if you check the Student class in folder /target/classes/com/huongdanjava/lombok, you will see the content as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.huongdanjava.lombok; import java.beans.ConstructorProperties; public class Student { private String firstName; private String middleName; private String lastName; private String country; @ConstructorProperties({"firstName", "middleName", "lastName", "country"}) Student(String firstName, String middleName, String lastName, String country) { this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; this.country = country; } public static Student.StudentBuilder builder() { return new Student.StudentBuilder(); } public String getFirstName() { return this.firstName; } public String getMiddleName() { return this.middleName; } public String getLastName() { return this.lastName; } public String getCountry() { return this.country; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setCountry(String country) { this.country = country; } public String toString() { return "Student(firstName=" + this.getFirstName() + ", middleName=" + this.getMiddleName() + ", lastName=" + this.getLastName() + ", country=" + this.getCountry() + ")"; } public static class StudentBuilder { private String firstName; private String middleName; private String lastName; private String country; StudentBuilder() { } public Student.StudentBuilder firstName(String firstName) { this.firstName = firstName; return this; } public Student.StudentBuilder middleName(String middleName) { this.middleName = middleName; return this; } public Student.StudentBuilder lastName(String lastName) { this.lastName = lastName; return this; } public Student.StudentBuilder country(String country) { this.country = country; return this; } public Student build() { return new Student(this.firstName, this.middleName, this.lastName, this.country); } public String toString() { return "Student.StudentBuilder(firstName=" + this.firstName + ", middleName=" + this.middleName + ", lastName=" + this.lastName + ", country=" + this.country + ")"; } } } |
You can see the new Student.StudentBuilder class was added.
Now, you can use the StudentBuilder class as below:
1 2 3 4 5 6 7 8 |
Student student = Student.builder() .firstName("Khanh") .middleName("Huu") .lastName("Nguyen") .country("Viet Nam") .build(); System.out.printf(student.toString()); |
Result: