Skip to content

Instantly share code, notes, and snippets.

@josemartinez111
Last active November 27, 2021 03:07
Show Gist options
  • Select an option

  • Save josemartinez111/a7e39b9a550d2155fe4b34d3a2031c8b to your computer and use it in GitHub Desktop.

Select an option

Save josemartinez111/a7e39b9a550d2155fe4b34d3a2031c8b to your computer and use it in GitHub Desktop.
A manual example of the builder pattern without Lombok πŸ˜‘πŸ˜«πŸ”«πŸ”₯πŸš’πŸ§―
public class BuilderMain {
public static void main(String[] args) {
Employee employee = new Employee.EmployeeBuilder("Cristiano", "Ronaldo", 33, 7)
.setPhone("0045-1234556")
.setAddress("Juventus")
.setMail("CR@Juventus.org").build();
}
}
package main.builderPattern;
public class Employee {
private final String firstName; //required
private final String lastName; //required
private final int age; //required
private final int personalId; // required
private final String phone; //optional
private final String address; //optional
private final String mail; //optional
public static class EmployeeBuilder {
private final String firstName; //required
private final String lastName; //required
private final int age; //required
private final int personalId; // required
private String phone; //optional
private String address; //optional
private String mail; //optional
public EmployeeBuilder(String firstName, String lastName, int age, int personalId) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.personalId = personalId;
}
public EmployeeBuilder setAddress(String address) {
this.address = address;
return this;
}
public EmployeeBuilder setPhone(String phone) {
this.phone = phone;
return this;
}
public EmployeeBuilder setMail(String mail) {
this.mail = mail;
return this;
}
public Employee build() {
// call the private constructor in the outer class
return new Employee(this);
}
}
private Employee(EmployeeBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.personalId = builder.personalId;
this.phone = builder.phone;
this.address = builder.address;
this.mail = builder.mail;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public int getPersonalId() {
return personalId;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
public String getMail() {
return mail;
}
}

What Is the Builder Pattern?

First, Let’s see how we can implement a builder design pattern.

1. First of all, you need to create a public static nested class, which has all the instance attributes from the outer class. The naming convention for Builder usually is that and if the class name is Employee, then the builder class should be named as EmployeeBuilder.

2. The outer class Employee should have a private constructor that takes a EmployeeBuilder object as its argument.

3. The builder class should have a public constructor with all the required attributes as parameters and these required attributes are defined as "final," which have setter methods to set the optional parameters. It should return the same Builder object after setting the optional attribute.

4. The final step is to provide a build() method in the builder class that will return the outer class object to the client. This build() method will call the private constructor in the outer class, passing the Builder object itself as the parameter to this private constructor.

5. The Employee class has only getter methods and no public constructor. So, the only way to get an Employee object is through the nested EmpolyeeBuilder class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment