1.一次性传入所有参数

例如对于一个有5个参数的类:

    public class Obj{

        A a;
        B b;
        C c;
        D d;
        E e;

        public Obj(A a,B b,C c,D d,E e){
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
            this.e = e;
        }   
    }

弊端:不得不输入所有参数,即使是只需要1个参数。

2.通过重叠参数

例如对于一个有5个参数的类:

    public class Obj{

        A a;
        B b;
        C c;
        D d;
        E e;

        public Obj(A a){
            this.a = a;
        }
        public Obj(A a,B b){
            this.a = a;
            this.b = b;
        }
        ...
        ...
        //直到列举完所有参数
    }

弊端:不优雅,影响可读性。

3.通过增加一个建造者可伸缩的构建

    public class Obj{

        A a;//必要参数
        B b;
        C c;
        D d;
        E e;

        private Obj(Bulider builder){
            this.a = builder.a;
            this.b = builder.b;
            this.c = builder.c;
            this.d = builder.d;
            this.e = builder.e;
        }
        static class Builder{
            A a;//必要参数
            B b;
            C c;
            D d;
            E e;

            public Builder(A a){
                this.a = a
            }
            //每次返回Builder本身
            public Builder b(B b){
                this.b = b;
                return this;
            }

            public Builder c(C c){
                this.c = c;
                return this;
            }
            ...
            ...
            //最后通过create()创建Obj对象,并传入Builder对象
            public Obj create(){
                return new Obj(this);
            }
        }

    }

4.建造设模式举例

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

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

        static class Builder{
            private String name;
            private int age;
            private String sex;
            private String address;
            public Builder(String name){
                this.name = name;
            }
            public Builder age(int age){
                this.age = age;
                return this;
            }
            public Builder sex(String sex){
                this.sex = sex;
                return this;
            }
            public Builder address(String address){
                this.address = address;
                return this;
            }
            public Student create(){
                return new Student(this);
            }
        }
    }

    //测试类
    public class Test {
        public static void main(String[] args) {
            Student student = new Student.Builder("八戒").age(10).create();
            System.out.println(student.toString());
        }
    }


    //输出

    Student{name='八戒', age=10, sex='null', address='null'}

这样当有大量参数是可选的时候,通过这种方法创建对象,会大大增加可读性。

网络上志同道合,我们一起学习网络安全,一起进步,QQ群:694839022