java common design pattern 1 -- Builder pattern

Builder mode is also commonly used in Android development. Usually we use this mode when building a complex object, which can separate the construction of complex object from its representation.

Characteristic:
Builder mode is usually a chain call. The key point is that each set method returns itself, that is, return this

Code example:
If we want to build a student object, we need to set age, gender, height and other parameters for this student

public class Student {
    private String name;
    private String sex;
    private String age;

    public Student() {
    }

    public Student(String name, String sex, String age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

builder class:

public class StudentBuilder {

    private String name;
    private String sex;
    private String age = "";

    private StudentBuilder() {//Privatize the constructors of StudentBuilder so that the external cannot access the internal properties
        //The only way to set properties is through the Builder object, which means that users can only construct StudentConfig objects through the Builder object
        //This is the separation of construction and presentation
    }

    public static class Builder {
        private String name;//Must pass parameter
        private String sex;//Must pass parameter
        private String age = "";//Optional parameters

        public Builder setName(String name) {
            this.name = name;
            return this;//Return to itself is the key to realize chain call
        }

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

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

        public Student create() {
            Student student = new Student();
            student.setAge(age);
            student.setName(name);
            student.setSex(sex);
            return student;
        }
    }
}

Comparison between using and not using builder mode:

        //The traditional method 1 constructor passes parameters, which is suitable for objects with few parameters
        Student student1 = new Student("xiaoxiao", "female", "19");
        //Traditional way 2 pass on one by one
        Student student2 = new Student();
        student2.setName("Little");
        student2.setSex("female");
        student2.setAge("19");
        //Builder mode
        Student student3 = new StudentBuilder.Builder()
                .setAge("19")
                .setName("xiaoxiao")
                .setSex("female")
                .create();

When the object is very complex and there are many parameter types, the chain call using builder mode will be clearer.

Keywords: Android

Added by l!m!t on Fri, 03 Apr 2020 00:00:18 +0300