Android Design Mode - Builder Mode

Builder mode is also a very common mode, such as AlertDialog in Android, which uses Builder design mode. Here is an example to illustrate.

1. First let's create a Person class

class Persion {
    public String name; //Full name
    public int age; //Age
    public int income; //income

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getIncome() {
        return income;
    }

    public void setIncome(int income) {
        this.income = income;
    }
}

2. For a more convenient assignment, we may also add a construction method

    public Persion(String name, int age, int income) {
        this.name = name;
        this.age = age;
        this.income = income;
    }

3. Let's assign a value to this Persion class

    public static void main(String[] args) {
        Persion persion = new Persion("Lv Kaixuan",23,8888);
    }

4. If someone else uses your Perion class, well...The first parameter should be the name, the second parameter...Guess it might be age, what's the third ghost? Then you have to stamp in the source code to see, for example, to let you know that if you write this, the code is not very readable. Here's how to write it using Builder mode

class Persion {
    public String name; //Full name
    public int age; //Age
    public int income; //income

    private Persion(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.income = builder.income;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getIncome() {
        return income;
    }

    public void setIncome(int income) {
        this.income = income;
    }

    //Static Internal Class
    public static class Builder{
        private String name; //Full name
        private int age; //Age
        private int income; //income

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

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

        public Builder income(int income) {
            this.income = income;
            return this;
        }

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

5. You can see that we have defined a static internal class and then use private constructors to receive and assign values to it

    private Persion(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.income = builder.income;
    }

6. Let's assign a value to the Persion class

        new Persion.Builder()
                .name("Lv Kaixuan")
                .age(23)
                .income(8888)
                .build();

Comparing the above, you can see that it greatly improves the readability of the code and makes it easier for others to understand. AlertDialog uses this pattern, and interested partners can read the source code.

Tags: Android

Posted on Sun, 12 Jul 2020 12:26:30 -0400 by centerwork