Android Builder Model

Keywords: Mobile OkHttp

In our daily research and development, the builder model is one of the many models used. In fact, a lot of times we unconsciously use the builder model designed by others to benefit from it. For example, Gson, okHttp, Retrift. The following code:

Gson gson = new GsonBuilder()
          .registerTypeAdapter(String.class,new StringAdapter())
          .registerTypeAdapter(Integer.class,new IntegerAdapter())
          .registerTypeAdapter(int.class,new IntegerAdapter())
          .create();

First through Builder and then set some parameters you want to set, and finally through create() get the desired object. Compared with the usual constructors, setter s and other settings, the advantages are chain calls, clear parameters to be set, and clear timing of returning objects.

Then we can also refer to this design to design our own humans to build for the builder model.

Design a Student class as follows:

public class Student {
    private final String name;
    private final String age;
    private final String phone;
    private final String address;
    private Student(Builder builder){
        this.name = builder.name;
        this.address = builder.address;
        this.age = builder.age;
        this.phone = builder.phone;
    }

    public String getName() {
        return name;
    }

    public static class Builder{
        private final String name;
        private final String age;
        private String phone;
        private String address;
        public Builder(String name,String age){
            this.name = name;
            this.age = age;
        }
        public Builder setPhone(String phone){
            this.phone = phone;
            return this;
        }
        public Builder setAddress(String address){
            this.address = address;
            return this;
        }
        public Student build(){
            Student student = new Student(this);
            if(student.getName().equals("calm")){
                throw new IllegalStateException("calm Is an illegal user");
            }
            return student;
        }
    }

    @Override
    public String toString() {
        return "name = "+name+" age = "+age+" phone = "+phone+" address = "+address;
    }
}

The core idea is the static Bulider class inside. We put the necessary parameters in the constructor of Builder, and the non-essential parameters are set by chain call. Finally, the Student object is returned through the build() method. The build() method calls the Student's private constructor, which takes the Builder as a parameter. It should be noted that if the parameters of the returned object are to be validated legally, the object must be constructed before validation. Because in the process of construction, it is non-thread-safe, which means that if it is not detected before construction, it may be changed by other threads and lose the significance of detection.

Finally, the code for the call is attached:

Student student = new Student.Builder("Calm","33")
                .setAddress("China")
                .setPhone("19999999999")
                .build();

This is the only way to record how the builder pattern is used.

Posted by purinkle on Wed, 23 Jan 2019 12:42:13 -0800