Builder Pattern
- is a type of creational design pattern that utilizes cascade method pattern and/or method chaining
Example Code
Problem: designing a class with multiple fields that are required and/or optional, without several constructors nor allowing invalid object states, and maintaining readability
public static void main(String args[]) {
Employee e1 = Employee.builder(1234, "Marcus Chiu")
.comment("hello, world!")
.description("this is the most talented guy in the entire world")
.build();
Employee e2 = Employee.builder(1235, "Erina Chiu")
.description("sister of marcus chiu")
.build();
}public class Employee {
// required
private Integer id;
private String name;
// optional
private String description;
private String comment;
public static EmployeeBuilder build(Integer id, String name) {
return new EmployeeBuilder(id, name);
}
public static class EmployeeBuilder {
private Integer id;
private String name;
private String description;
private String comment;
public EmployeeBuilder(Integer id, String name) {
this.id = id;
this.name = name;
}
public EmployeeBuilder description(String description) {
this.description = description;
return this;
}
public EmployeeBuilder comment(String comment) {
this.comment = comment;
return this;
}
public Employee build() {
Employee e = new Employee();
e.id = this.id;
e.name = this.name;
e.description = this.description;
e.comment = comment;
return e;
}
}
}