Friday, 24 May 2019

Builder Design Pattern in Java

         Design Patterns are an important interview topic for experienced Java developers (4+ years of experience). For freshers, interviewers typically focus on the Singleton and Factory Design Patterns. However, for experienced candidates, it is important to have a good understanding of other design patterns as well.

In this post, we will discuss one of the most widely used design patterns: the Builder Design Pattern.


Read Top Java blogs, Feedspot Top 40 Java Blogs


What is Builder Design Pattern? 

According to wikipedia definition:-

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

It is used to construct a complex object step by step and the final step will return the object.


When to use ?

 The Builder Design Pattern is useful in the following scenarios:
  • When an object can have multiple representations.

  • When a constructor requires a large number of parameters (typically more than 4 or 5).

  • When object creation involves optional parameters.


Guidelines for Creating the Builder Design Pattern:-

  1. Create a static builder class inside the class whose object is to be built. In the example, EmployeeBuilder is a static nested class inside the Employee class.

  2. The builder class should have a public constructor that accepts the required parameters, while the outer class should have a private constructor. All fields in the outer class should be declared as private.

  3. Create methods in the builder class to set the values of the optional parameters, for example, withPhone(), withAddress(), and withDateOfBirth(). In this example, phone, address, and dateOfBirth are optional fields.

  4. Create a build() method in the builder class to construct and return the desired object.

Example:--

Employee.java,

package com.test;

public class Employee {
 
     private int id;
     private String name;
     private int age;
     private String phone;
     private String address;
     private String dob;
 
     private Employee(EmployeeBuilder builder) {
         this.id = builder.id;
         this.name = builder.name;
         this.age = builder.age;
         this.phone = builder.phone;
         this.address = builder.address;
         this.dob = builder.dob;
     }
 
     public int getId() {
         return id;
     }

     public String getName() {
         return name;
     }

     public int getAge() {
         return age;
     }

     public String getPhone() {
         return phone;
     }

     public String getAddress() {
         return address;
     }

     public String getDob() {
         return dob;
     }

     public static class EmployeeBuilder {
  
         private int id;
         private String name;
         private int age;
         private String phone;
         private String address;
         private String dob;
  
         //id, name and age are manadatory fields 
         public EmployeeBuilder(int id, String name, int age) {
             this.id = id;
             this.name = name;
             this.age = age;
         }
  
         public EmployeeBuilder withFieldPhone(String phone) {
             this.phone = phone;
             return this;
         }
  
         public EmployeeBuilder withFieldAddress(String address) {
             this.address = address;
             return this;
         }
  
         public EmployeeBuilder withFieldDateOfBirth(String dob) {
             this.dob = dob;
             return this;
         }
  
         public Employee build() {
             return new Employee(this);
         }
    }    
 
     @Override 
     public String toString() { 
         return "Id - "+id+", Name - "+name+", age - "+age+ ",address - "+ address+
          ", Phone - "+phone; 
     }
  
}

The above code demonstrates the implementation of the Builder Design Pattern. You can create and access an Employee object using the builder, as shown in the following code.

BuilderDesignPatternEx.java,

package com.test;

public class BuilderDesignPatternEx {
 
     public static void main(String[] args) {
         Employee pattern = new Employee.EmployeeBuilder(10, "Kiran",10).withFieldAddress("Sonyal").build();
         System.out.println(pattern);
         //System.out.println(pattern.getName());
     }

}

Output:--
Id - 10, Name - Kiran, age - 10,address - Sonyal, Phone - null

Java Built-in Examples of the Builder Design Pattern:-

The StringBuilder and StringBuffer classes are common examples of the Builder Design Pattern in Java. They allow you to construct and modify strings step by step using a fluent API before obtaining the final result.


Thank you for visiting the blog!

No comments:

Post a Comment