Java -Builder Design Pattern with example

The builder pattern, as the name implies, is an alternative way to construct complex objects. This should be used only when you want to build different immutable objects using the same object building process.
The builder pattern is used to create instances of a very complex object having telescoping constructors in the easiest way.
Method Chaining: In java, Method Chaining is used to invoke multiple methods on the same object which occurs as a single statement. Method-chaining is implemented by a series of methods that return this reference for a class instance.
Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.
Advantages
  • It provides a clear separation between the construction and representation of an object.
  • It provides better control over the construction process.
  • It supports to change the internal representation of objects.
Implementation
Constructors in Java are used to create objects and can take parameters required to create objects. Let's see an example and learn how to implement a builder pattern. Consider a POJO of Employee below.

Employee POJO Class

public class Employee {
// All final attributes
private final String firstName; // required
private final String lastName; // required
private final int age; // optional
private final String phone; // optional
private final String address; // optional

private Employee(EmployeeBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}

// All getter, and NO setter to provde immutability
public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public int getAge() {
return age;
}

public String getPhone() {
return phone;
}

public String getAddress() {
return address;
}

@Override
public String toString() {
return "Employee: " + this.firstName + ", " + this.lastName + ", " + this.age + ", " + this.phone + ", "
+ this.address;
}

public static class EmployeeBuilder {
private final String firstName;
private final String lastName;
private int age;
private String phone;
private String address;

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

public EmployeeBuilder age(int age) {
this.age = age;
return this;
}

public EmployeeBuilder phone(String phone) {
this.phone = phone;
return this;
}

public EmployeeBuilder address(String address) {
this.address = address;
return this;
}

// Return the finally constructed Employee object
public Employee build() {
Employee employee = new Employee(this);
validateEmplopyeeObject(employee);
return employee;
}

private void validateEmplopyeeObject(Employee employee) {
// Do some basic validations to check
// if employee object does not break any assumption of system
}
}
}

Driver Class

public class BuilderPatternDemo {
public static void main(String[] args) {
System.out.println(employee1);
Employee employee2 = new Employee.EmployeeBuilder("July", "Reacher").age(40).phone("2132134242")
System.out.println(employee2);
Employee employee3 = new Employee.EmployeeBuilder("He", "Man")
System.out.println(employee3);

Employee employee1 = new Employee.EmployeeBuilder("Sibin", "Scott").age(30).phone("9898989898")
.address("YYYYYYYYYYY").build();
// no address
.build();
// No age
// No phone
// no address
.build();
}
}

Output:

Employee: Sibin, Scott, 30, 9898989898, YYYYYYYYYYY
Employee: July, Reacher, 40, 2132134242, null
Employee: He, Man, 0, null, null

by sibin

Comments

  1. Don't miss the Java Jobs opportunities, there are many genuine and free Java jobs info shared every day, click and join the group: https://chat.whatsapp.com/HLFm93tkHQR4zu8hafpqSl

    ReplyDelete
  2. Design patterns and system design are concepts every professional developers should learn. These are some commonly asked system design interview questions. Thanks for sharing this example. Keep sharing such educational articles.

    ReplyDelete

Post a Comment