builder设计模式详解

在网上看到很多介绍builder模式的文章,但总感觉难以理解,直到看了《Effective Java》一书对该模式的介绍,恍然大悟。
  要介绍builder模式,不得不先讲解另外两种设计模式,帮助我们理解,分别是重叠构造器模式和java Bean模式,下面我会分别介绍。
  如果让我们用一个类表示一个学生的信息,学生信息中姓名是必须的,且不可以修改,其他的域可选,有手机号、年龄、学院、专业、班级、寝室号等。我们先用重叠构造器模式来实现。这种模式,一般你需要提供一个只有必要参数的构造器,第二个有一个可选参数的构造器,第三个有两个可选参数的构造器,以此类推。

public class Student {
    private final String name;
    private final String mobile;
    private Integer age;
    private String school;
    private String grade;
    private String roomNumber;
    public Student(String name, String mobile) {
        this(name, mobile, null);
    }
    public Student(String name, String mobile, Integer age) {
        this(name, mobile, age, null);
    }
    public Student(String name, String mobile, Integer age, String school) {
        this(name, mobile, age, school, null);
    }
    public Student(String name, String mobile, Integer age, String school, String grade) {
        this(name, mobile, age, school, grade, null);
    }
    public Student(String name, String mobile, Integer age, String school, String grade, String roomNumber) {
        this.name = name;
        this.mobile = mobile;
        this.age = age;
        this.school = school;
        this.grade = grade;
        this.roomNumber = roomNumber;
    }
}

调用者实例化时需要选择对应的构造器:

Student sutdent = new Student("name", "13500000001", 18);

这个构造器可能会存在你不想设置的参数。调用者调用也会很不方便,如果弄错了参数顺序,编译器也不会出错。而且当参数很多的时候,代码会很多,很乱。重叠构造器最大的缺点就是不能很好的扩展大量的可选参数,而且难以阅读。
  遇到大量可选参数时,我们可以考虑使用另一种设计模式,即JavaBean模式,想必大家应该很熟悉了。

public class Student {
    private String name;
    private String mobile;
    private Integer age;
    private String school;
    private String grade;
    private String roomNumber;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    public String getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    public String getRoomNumber() {
        return roomNumber;
    }
    public void setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
    }
}

调用者调用时,只需要调用无参构造器实例化对象,并调用set方法设置参数:

Student student = new Student();
student.setName("name");
student.setMobile("13500000001");
student.setAge(18);

很明显,这样的代码创建实例更加容易,可读性也更好了。但是JavaBean也有他的不足,调用者需要调用多个set方法设置参数,如果参数过多,代码也会比较庞大。另外,JavaBean模式无法像重叠构造器那样保证参数的不可变性,即无法使用final修饰参数。
  Builder模式很好的解决了这个问题,它既能保证像重叠构造器模式那样的参数安全性,也能保证代码的可读性。

public class Student {
    private final String name;
    private final String mobile;
    private Integer age;
    private String school;
    private String grade;
    private String roomNumber;
    public Student(StudentBuilder builder) {
        this.name = builder.name;
        this.mobile = builder.mobile;
        this.age = builder.age;
        this.school = builder.school;
        this.grade = builder.grade;
        this.roomNumber = builder.roomNumber;
    }
    public String getName() {
        return name;
    }
    public String getMobile() {
        return mobile;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    public String getGrade() {
        return grade;
    }
    public void setGrade(String grade) {
        this.grade = grade;
    }
    public String getRoomNumber() {
        return roomNumber;
    }
    public void setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
    }
    public static class StudentBuilder implements Builder {
        private final String name;
        private final String mobile;
        private Integer age;
        private String school;
        private String grade;
        private String roomNumber;
        public StudentBuilder(String name, String mobile) {
            this.name = name;
            this.mobile = mobile;
        }
        public StudentBuilder setAge(Integer age) {
            this.age = age;
            return this;
        }
        public StudentBuilder setSchool(String school) {
            this.school = school;
            return this;
        }
        public StudentBuilder setGrade(String grade) {
            this.grade = grade;
            return this;
        }
        public StudentBuilder setRoomNumber(String roomNumber) {
            this.roomNumber = roomNumber;
            return this;
        }
        @Override
        public Student build() {
            return new Student(this);
        }
    }
}

调用者创建Student实例通过StudentBuilder就可以了,builder的set方法返builder本身,可以把调用连接起来。下面是代用示例:

new StudentBuilder("name", "13500000001")
                .setAge(18).setSchool("school").build();

这样的调用代码是不是更简洁一些。同时我们还可以在Builde中增加对参数一些必要的参数校验。

你可能感兴趣的:(builder设计模式详解)