Big Talk Design Patterns: The Builder Pattern

The builder pattern is suitable for situations where there are too many object properties

There is a class with a lot of properties: Person

public class Person {
    
    private String a;
    private String b;
    private String c;
    private String d;
    private String e;
    private String f;
    private String g;
    private String h;
    private String i;
    private String j;
    
    // get/set methods
    
    // Constructor 1
    // Constructor 2
    // Constructor 3
    // Constructor 4
    // Constructor 5
    // Constructor 6
    
}

There are many attributes of the Person class. When we create Persons with different attributes, we need to write a lot of construction methods to deal with different scenarios, so the code is very bloated. There are two solutions, one is to use the BeanSet method, and the other is to use the Build method. model.

BeanSet method

public class Person {
    
    private String a;
    private String b;
    private String c;
    private String d;
    private String e;
    private String f;
    private String g;
    private String h;
    private String i;
    private String j;
    
    public void setA(String a) {
        this.a = a;
    }
    
    // ......
    
}

This BeanSet method is a commonly used method. Now the plug-in industry is very developed. The famous Lombok automatically generates get and set methods for us, which saves us the time to write get and set methods, and the code looks very clean. However, in our When creating an object, you have to do this:

    public static void main(String[] args) {
        Person person = new Person();
        person.setA("");
        person.setB("");
        person.setC("");
        person.setD("");
        person.setE("");
        person.setF("");
        person.setG("");
        person.setH("");
        person.setI("");
        person.setJ("");
    }

There's nothing wrong with writing this way, but it always feels a little bit worse.

Builder mode

Standard notation

  1. An abstract builder, typically an interface that contains methods for creating product objects
  2. Director, call specific builder, create product object
  3. specific builder
  4. Generate product object

The advantages of this writing method are cumbersome and rarely used in daily work.

General spelling

The general writing omits the abstract builder and director, and uses the static inner class to create the builder.

public class User {

    private String name;

    private Integer age;

    private String gender;

    private String mobile;

    private String idCard;

    private String address;

    private User(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.gender = builder.gender;
        this.mobile = builder.mobile;
        this.idCard = builder.idCard;
        this.address = builder.address;
    }

    public static class Builder {

        /**require**/
        private final String name;

        private final Integer age;

        /**optional**/
        private String gender;

        private String mobile;

        private String idCard;

        private String address;

        public Builder(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public Builder gender(String gender) {
            this.gender = gender;
            return this;
        }

        public Builder mobile(String mobile) {
            this.mobile = mobile;
            return this;
        }

        public Builder idCard(String idCard) {
            this.idCard = idCard;
            return this;
        }

        public Builder address(String address) {
            this.address = address;
            return this;
        }

        public User build() {
            return new User(this);
        }

    }

}

How to use

public static void main(String[] args) {
        User use = new Builder("zhangsan", 12).address("beijing").build();
}

Posted by gckmac on Tue, 17 May 2022 10:20:24 +0300