美文网首页
Builder模式

Builder模式

作者: 古早味蛋糕 | 来源:发表于2022-12-03 12:32 被阅读0次
    1 为什么要用Builder模式

    Builder模式主要用于解决初始化类时(也就是new一个类的实例出来),类的构造函数种类过多且不易管理的问题。
    有3个参数的类能构建出多少个构造函数:

        public class Student {
            private String name;
            private int age;
            private boolean sex;
        
            public Student() {
            }
        
            public Student(String name) {
                this.name = name;
            }
        
            public Student(int age) {
                this.age = age;
            }
        
            public Student(boolean sex) {
                this.sex = sex;
            }
        
            public Student(String name, int age) {
                this.name = name;
                this.age = age;
            }
        
            public Student(String name, boolean sex) {
                this.name = name;
                this.sex = sex;
            }
        
            public Student(int age, boolean sex) {
                this.age = age;
                this.sex = sex;
            }
        
            public Student(String name, int age, boolean sex) {
                this.name = name;
                this.age = age;
                this.sex = sex;
            }
        
            public Student(StudentBuilder studentBuilder) {
                this.name = studentBuilder.name;
                this.age = studentBuilder.age;
                this.sex = studentBuilder.sex;
            }
        }
    

    一共有9种之多。如果这些构造函数都写到类里面,不但代码量大、代码不美观,而且调用起来也容易出错。

    2 Builder模式的实现

    有的需求是Student类的构造函数不要那么多,但是又要满足初始化Student类变量。
    可以考虑设计一个内部类,这个内部类的参数跟Student类的参数一样,而Student类的构造函数的参数,我们就设定为这个内部类。
    因此,只需要将这个内部类的变量初始化即可。内部类变量设定的时候,我们采用链式结构,这样可以通过setxx().setxx.()形式一直写下去。目前RxJava、OkHttp等框架均采用了这样的链式结构设计。

    public class Student {
    
        private String name;
        private int age;
        private boolean sex;
    
        public Student() {
        }
    
        public Student(StudentBuilder studentBuilder) {
            this.name = studentBuilder.name;
            this.age = studentBuilder.age;
            this.sex = studentBuilder.sex;
        }
    
        public static StudentBuilder newInstance() {
            return new StudentBuilder();
        }
    
        public static class StudentBuilder {
            private String name;
    
            private int age;
    
            private boolean sex;
    
            public StudentBuilder setName(String name) {
                this.name = name;
                return this;
            }
    
            public StudentBuilder setAge(int age) {
                this.age = age;
                return this;
            }
    
            public StudentBuilder setSex(boolean sex) {
                this.sex = sex;
                return this;
            }
    
            public Student build() {
                return new Student(this);
            }
        }
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex=" + sex +
                    '}';
        }
        
        public Student setName(String name) {
            this.name = name;
            return this;
        }
    
        public Student setAge(int age) {
            this.age = age;
            return this;
        }
    
        public Student setSex(boolean sex) {
            this.sex = sex;
            return this;
        }
    }
    

    有3种使用方法可以实现Builder模式,代码如下:

    Builder模式调用的过程清晰、代码简洁明了。

     public static void main(String[] args) {
           //第一种
           Student student1 = new Student.StudentBuilder().setName("张三").setAge(18).
                    setSex(true).build();
           //第二种
           Student student2 = Student.newInstance().setName("李四").setAge(20).setSex(false).
                    build();
           //第三种
           Student student3 = new Student().setAge(22).setName("王五").setSex(true);
    
            System.out.println("student1= "+student1.toString());
            System.out.println("student2= "+student2.toString());
            System.out.println("student3= "+student3.toString());
        }
    

    相关文章

      网友评论

          本文标题:Builder模式

          本文链接:https://www.haomeiwen.com/subject/bradfdtx.html