The builder pattern of use project

1 issue

The builder pattern, we may not be familiar, because we see a lot of open source frameworks or Android source code used inside, like this code structure

A a = new A.builder().method1("111").method2("222").build();

Obviously, the general structure where there builder () constructor, as well as build () function is mainly used to doing it? Not for building object, if you need to set many parameters, then one by one to set a very complicated does not look good, here's an example showing simple and crude. 

 

 

 

 

 

2 Test code implementation

package com.example.test1;

public class Student {
    private int id;
    private String name;
    private int age;
    private String classNmae;

    public Student(Builder builder) {
        this.id = builder.id;
        this.name = builder.name;
        this.age = builder.age;
        this.classNmae = builder.classNmae;
    }
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getClassNmae() {
        return classNmae;
    }

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

    public static class Builder {
        private int id;
        private String name;
        private int age;
        private String classNmae;

        public Builder() {}
        public Builder id(int id) {
            this.id = id;
            return this;
        }
        public Builder name(String name) {
            this.name = name;
            return this;
        }
        public Builder age(int age) {
            this.age = age;
            return this;
        }
        public Builder className(String classNmae) {
            this.classNmae = classNmae;
            return this;
        }
        public Student build() {
            return new Student(this);
        }
    }
}
                Student student = new Student.Builder()
                .name("chenyu")
                .age(28)
                .id(1)
                .className("erzhong")
                .build();
        Log.i(TAG, "student toString is:" + student.toString());

 

 

 

3 operating results

18376-18376/com.example.test1 I/chenyu: student toString is:Student{id=1, name='chenyu', age=28, classNmae='erzhong'}

 

 



4 Summary

1) We need to build a lot of parameters when an object in this way

2) the most critical is the need to build an object class which has a class passed as an argument Builder

    public Student(Builder builder) {
        this.id = builder.id;
        this.name = builder.name;
        this.age = builder.age;
        this.classNmae = builder.classNmae;
    }

3) Finally, build () function which returns the class itself needs to be built

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

 

Released 1075 original articles · won praise 680 · Views 3.02 million +

Guess you like

Origin blog.csdn.net/u011068702/article/details/105058815