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
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.
Create a static builder class inside the class whose object is to be built. In the example,
EmployeeBuilderis a static nested class inside theEmployeeclass.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.Create methods in the builder class to set the values of the optional parameters, for example,
withPhone(),withAddress(), andwithDateOfBirth(). In this example,phone,address, anddateOfBirthare optional fields.Create a
build()method in the builder class to construct and return the desired object.
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;
}
}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:-
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.
No comments:
Post a Comment