Friday, 24 May 2019

Builder Design Pattern in Java

        The  Design Pattern is an important question for the experienced(4+ exp) candidates.  For freshers interviewer will check the Singleton or Factory design pattern knowledge but when comes to experience we need to know other design patterns also. In the current post, we will discuss one of the important design pattern i.e 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 ?

 Following are the conditions where we can use builder design pattern,
  • When multiple representation of objects are required.
  • Number of parameter required in constructor is more than manageable(more than 4 or 5)
  • Object creation contains optional parameter.

Guidelines to create the Builder Design Pattern:-

1) Create a static builder class inside the class whose object is to be build by the builder. EmployeeBuilder class is a static class created as in the example.

2) Builder class should have public constructor  with required parameters and outside class should have private constructor. All fields of class should be private.

3) Needs to write methods to get the values of optionalparameters. 
e.g withFieldPhone(), withFieldAddress and withFieldDateOfBirth().
In example, phone,address and dob are optional parameters.

4) Create builder method in the builder class to get 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 is an example of Builder Design pattern, Now this we can access in below 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 builder design pattern examples:

StringBuilder and StringBuffer classes are internally used builder design pattern concept.

Thank you for visiting blog..

No comments:

Post a Comment