Sitemap
Avyudaya

Avyudaya

Builder Design Pattern

3 min readJul 18, 2024

Our very first Creational Design Pattern

The builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.

Remember a scenario where you are trying to instantiate an object of the class. The class has a lot of fields that have to be set up for the object to be initialized properly or the process is very cumbersome.

Our class Definition:

public class Person {
private final String firstName;
private final String middleName;
private final String lastName;
private final LocalDate dateOfBirth;
private final String emailAddress;
private final String phoneNumber;


public Person(String firstName, String middleName, String lastName, LocalDate dateOfBirth, String emailAddress, String phoneNumber) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.emailAddress = emailAddress;
this.phoneNumber = phoneNumber;
if (firstName == null || lastName == null) {
throw new IllegalArgumentException("First name and last name must not be null");
}
}

@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", middleName='" + middleName + '\'' +
", lastName='" + lastName + '\'' +
", dateOfBirth=" + dateOfBirth +
", emailAddress='" + emailAddress + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
}

To instantiate the object of the class we should do:

public class Main {
public static void main(String[] args) {
Person person1 = new Person("Avyudaya", "", "Acharya",
LocalDate.of(2000, 1, 26), "avyudaya7@gmail.com", "123456789");
System.out.println(person1);
}
}

A person reviewing your code will find it very difficult to understand that when we are passing parameters to the constructor what fields are being set. This is also very inefficient if there are multiple constructors for setting up different fields or for cases when some values are optional whereas some are compulsory.

Get Avyudaya’s stories in your inbox

Join Medium for free to get updates from this writer.

To solve this, let us use our very first creational design pattern, ie. the Builder design pattern. It falls under the Creational design pattern because it is used to create objects.

Our new class Definition:

public class Person {
private final String firstName;
private final String middleName;
private final String lastName;
private final LocalDate dateOfBirth;
private final String emailAddress;
private final String phoneNumber;

private Person(PersonBuilder personBuilder) {
this.firstName = personBuilder.firstName;
this.middleName = personBuilder.middleName;
this.lastName = personBuilder.lastName;
this.dateOfBirth = personBuilder.dateOfBirth;
this.emailAddress = personBuilder.emailAddress;
this.phoneNumber = personBuilder.phoneNumber;
if (firstName == null || lastName == null) {
throw new IllegalArgumentException("First name and last name must not be null");
}
}

@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", middleName='" + middleName + '\'' +
", lastName='" + lastName + '\'' +
", dateOfBirth=" + dateOfBirth +
", emailAddress='" + emailAddress + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}

public static class PersonBuilder {

private final String firstName;
private String middleName;
private final String lastName;
private LocalDate dateOfBirth;
private String emailAddress;
private String phoneNumber;

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

public PersonBuilder middleName(String middleName) {
this.middleName = middleName;
return this;
}

public PersonBuilder dateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
return this;
}

public PersonBuilder emailAddress(String emailAddress) {
this.emailAddress = emailAddress;
return this;
}

public PersonBuilder phoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
return this;
}

public Person build() {
return new Person(this);
}
}
}

We have added all the fields of the Person class in an inner static class called PersonBuilder. We have made this static and nested so that the builder class can’t be used by itself. Also, see that the firstName and lastName are marked as final because they are being passed on via the constructor. We have added optional methods to set other fields and a build method to build a Person object later using the PersonBuilder object.

Now we can instantiate the object using a more readable code:

public class Main {
public static void main(String[] args) {
var person1 = new Person.PersonBuilder("Avyudaya", "Acharya")
.dateOfBirth(LocalDate.of(2000, 1, 26))
.emailAddress("avyudaya7@gmail.com")
.build();
System.out.println(person1);
}
}

Hence, we can use the builder design in scenarios where the constructor has a lot of parameters and it becomes difficult to remember the required order of parameters as well as what particular constructor you might want in a given situation.

--

--