Builder Design Patterns
The definition of Builder Pattern provided by Gang of Four book on Design Patterns states:
The definition of Builder Pattern provided by Gang of Four book on Design Patterns states:
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Now test UserBuilder.
package com.shubh.builder.pattern;
public final class User {
// required
private final String firstName;
// required
private final String lastName;
// required
private final String email;
private final int age;
private final String phone;
private final String address;
private User(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.email = builder.email;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}
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;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "User [ First Name:" + this.firstName + ", Last Name: " + this.lastName + ", Email: " + this.email
+ ", Age: " + this.age + ", Phone: " + this.phone + ", Address: " + this.address + "]";
}
public static class UserBuilder {
private final String firstName;
private final String lastName;
private final String email;
private int age;
private String phone;
private String address;
public UserBuilder(String firstName, String lastName, String email) {
// as firstName, lastName, email are required fields
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public UserBuilder age(int age) {
this.age = age;
return this;
}
public UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
public UserBuilder address(String address) {
this.address = address;
return this;
}
// Return the finally consrcuted User object
public User build() {
User user = new User(this);
validateUserObject(user);
return user;
}
private void validateUserObject(User user) {
// Do some code here for basic validations if required
}
}
}
Now test UserBuilder.
package com.shubh.builder.pattern;
public class UserBuilderTest {
public static void main(String[] args) {
// Create user with all attributes.
User user1 = new User.UserBuilder("Sandeep", "kumar", "sandeep@xyz.com")
.age(30).phone("1234567")
.address("Test address 1234")
.build();
System.out.println(user1);
// Create user without address.
User user2 = new User.UserBuilder("Pankaj", "xyz", "pankaj@xyz.com")
.age(40)
.phone("5655")
// no address
.build();
System.out.println(user2);
// Create user only with required attributes
User user3 = new User.UserBuilder("Admin", "Test", "admin@xyz.com")
// No age
// No phone
// no address
.build();
System.out.println(user3);
}
}
Output of above program.
User [ First Name:Sandeep, Last Name: kumar, Email: sandeep@xyz.com, Age: 30, Phone: 1234567, Address: Test address 1234]
User [ First Name:Pankaj, Last Name: xyz, Email: pankaj@xyz.com, Age: 40, Phone: 5655, Address: null]
User [ First Name:Admin, Last Name: Test, Email: admin@xyz.com, Age: 0, Phone: null, Address: null]
Q- When and Why to use Builder Desing Pattern?
- Builder pattern is used to construct a complex object step by step and the final step will return the object.
- Once required immutable object.
- To avoids the problem of a constructor with a large number of parameters.
- Builders are immutable objects and should not be modified once instantiated.
- Builders are best suitable for objects that can be live throughout the application lifecycle.
- Use Builde where you need 'object that need other objects or "Parts" to construct them'.
- Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.
Q- What is the difference between builder and factory pattern? Or builder Vs factory pattern?
No comments:
Post a Comment