SoFunction
Updated on 2025-03-08

Introduction to the Builder pattern in Java design

Overview

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

Use scenarios

  • The same method, different execution order, produce different event results
  • When multiple components or parts can be assembled into one object, but the results of the operation are different.
  • Product classes are very complex, or the call order in the product classes has different effects.
  • When initializing an object is particularly complicated, such as many parameters, and many parameters have default values

The following is a Person example to analyze the use of overlap constructor mode, JavaBeans mode, and Builder mode. The Person class has two necessary parameters (id and name), and 5 optional parameters (age, sex, phone, address and desc)

Overlapping constructor mode

In this mode, you provide the first constructor with only the necessary parameters, the second constructor has one optional parameter, the third has two optional parameters, and so on, the last constructor contains all the optional parameters. Let's take a look at its programming implementation:

/ * Using Overlap Constructor Mode
 */
public class Person {
 //Required parameters private final int id;
 private final String name;
 //Optional parameters private final int age;
 private final String sex;
 private final String phone;
 private final String address;
 private final String desc;
 public Person(int id, String name) {
 this(id, name, 0);
 }
 public Person(int id, String name, int age) {
 this(id, name, age, "");
 }
 public Person(int id, String name, int age, String sex) {
 this(id, name, age, sex, "");
 }
 public Person(int id, String name, int age, String sex, String phone) {
 this(id, name, age, sex, phone, "");
 }
 public Person(int id, String name, int age, String sex, String phone, String address) {
 this(id, name, age, sex, phone, address, "");
 }
 public Person(int id, String name, int age, String sex, String phone, String address, String desc) {
  = id;
  = name;
  = age;
  = sex;
  = phone;
  = address;
  = desc;
 }
}

From the above code, when you want to create an instance, use the constructor with the shortest parameter list, but the list contains all the parameters to be set:

Person person = new Persion(1, "Li Si", 20, "male", "18800000000", "China", "Testing using overlap constructor mode");

Overlapping constructors work, but when there are many parameters, creating codes can be difficult to write and read

JavaBeans pattern

Using Javabean writing method, write a bunch of setter methods for attributes, generate objects, and then call the setter method to assign values ​​to the attributes.

/ * useJavaBeansmodel
 */
public class Person {
 //Required parameters private int id;
 private String name;
 //Optional parameters private int age;
 private String sex;
 private String phone;
 private String address;
 private String desc;
 public void setId(int id) {
  = id;
 }
 public void setName(String name) {
  = name;
 }
 public void setAge(int age) {
  = age;
 }
 public void setSex(String sex) {
  = sex;
 }
 public void setPhone(String phone) {
  = phone;
 }
 public void setAddress(String address) {
  = address;
 }
 public void setDesc(String desc) {
  = desc;
 }

This pattern makes up for the shortcomings of overlap constructor modes. Creating an instance is easy, and the resulting code is easy to read:

Person person = new Person();
(1);
("Li Si");
(20);
("male");
("18800000000");
("China");
("Testing with JavaBeans pattern");

Because the JavaBeans pattern is divided into several calls, JavaBeans may be in an inconsistent state during the construction process. Classes cannot guarantee consistency simply by checking the validity of constructor parameters.

Builder mode

The Builder mode can not only ensure security like the overlapping constructor, but also achieve readability like the JavaBean mode.

/**
  * Use Builder mode
  */
public class Person {
 //Required parameters private final int id;
 private final String name;
 //Optional parameters private final int age;
 private final String sex;
 private final String phone;
 private final String address;
 private final String desc;
 private Person(Builder builder) {
  = ;
  = ;
  = ;
  = ;
  = ;
  = ;
  = ;
 }
 public static class Builder {
 //Required parameters private final int id;
 private final String name;
 //Optional parameters private int age;
 private String sex;
 private String phone;
 private String address;
 private String desc;
 public Builder(int id, String name) {
   = id;
   = name;
 }
 public Builder age(int val) {
   = val;
  return this;
 }
 public Builder sex(String val) {
   = val;
  return this;
 }
 public Builder phone(String val) {
   = val;
  return this;
 }
 public Builder address(String val) {
   = val;
  return this;
 }
 public Builder desc(String val) {
   = val;
  return this;
 }
 public Person build() {
  return new Person(this);
 }
 }
}

Person cannot be changed, and all default parameter values ​​are placed separately in one place. The setter method of builder returns the builder itself so that it can be called chained. Here is the client usage code:

Person person = new (1, "Zhang San")
.age(18).sex("Male").desc("Test using builder mode").build();

Builder mode in Android source code

  • AlertDialog source code
  • Universal-Image-Loader Image Library
  • OkHttp's Request source code

advantage:

  • Good packaging, using builder mode allows clients to know the details of the internal composition of the product.
  • Builder independent and easy to expand (can be used through abstraction--interface, abstract class)

shortcoming:

  • Will generate excess Builder objects and Director objects, consuming memory

Summarize :The Builder pattern is usually used as a builder for configuration classes to separate the construction and representation of configurations, and isolate the configuration from the target class to avoid too many setter methods.