情景:当我们设计一个类,这个类有多个成员变量,有些必填有些非必填
例:一个user类,有5个成员变量,其中两个是必填的
1.重叠构造器
public class User {
private String name;//required
private int age;//required
private String address;//optional
private String sex;//optional
private double height;//optional
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public User(String name, int age, String address, String sex) {
this.name = name;
this.age = age;
this.address = address;
this.sex = sex;
}
public User(String name, int age, String address, String sex, double height) {
this.name = name;
this.age = age;
this.address = address;
this.sex = sex;
this.height = height;
}
/*another method 还可以写很多构造器这里就不写了*/
}
创建实例的时候:
User user = new User("yuze",21);
这种重叠构造器的方法是可行的,但是当参数很多的时候,代码会很难编写,且较难阅读,当一长串类型相同的参数会导致一些微妙的错误,如果不小心颠倒了其中两个参数的顺序,编译器不会出错但是程序运行就不是你想要的了
2.JavaBeans模式
遇到许多可选的构造器参数时,还有第二种代替方法:JavaBeans模式,这种方法是先调用一个无参构造器来创建对象然后再调用setter方法来设置每个必要的参数以及一些相关可选参数:
public class User {
private String name;//required
private int age;//required
private String address;//optional
private String sex;//optional
private double height;//optional
public User() {
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setHeight(double height) {
this.height = height;
}
}
这种模式弥补了重叠构造器的不足,创建对象很容易,阅读起来也很容易:
User user = new User();
user.setName("yuze");
user.setAge(21);
//...
遗憾的是,JavaBeans模式自身有着很严重的缺点,因为整个对象的构造过程被分到了几个调用中,在构造过程中JavaBeans可能处于不一致的状态?,类无法仅仅通过校验构造器参数的有效性来保证一致性。试图使用处于不一致的状态的对象会导致失败。另一点不足在于,JavaBeans模式使得把类做成不可变的可能性不复存在
3.建造者(Builder)模式
public class User {
private String name;//required
private int age;//required
private String address;//optional
private String sex;//optional
private double height;//optional
public static class Builder{
private String name;//required
private int age;//required
private String address;//optional
private String sex;//optional
private double height;//optional
public Builder(String name,int age){
this.name = name;
this.age = age;
}
public Builder address(String address){
this.address = address;
return this;
}
public Builder sex(String sex){
this.sex = sex;
return this;
}
public Builder height(double height){
this.height = height;
return this;
}
public User build(){
return new User(this);
}
}
public User(Builder builder){
name = builder.name;
age = builder.age;
address = builder.address;
sex = builder.sex;
height = builder.height;
}
}
User user = new Builder("yuze",21).address("china").sex("man").height(2.0).build();
显然,这样代码非常容易阅读,Builder模式模拟了具名的可选参数
其实,Builder模式也有自身的不足,为了创建对象,必须先创建它的构建起,Builder模式还比重叠构造器模式更加冗长,因此它只在很多参数的时候才使用
简而言之,如果类的构造器或者静态工厂中具有多个参数,设计这种类时,Builder模式就是一种不错的选择,与使用重叠构造器模式相比,使用Builder模式的代码将更易于阅读和编写,构建器也比JavaBeans更加安全
以上内容部分借鉴书记Effective Java,也推荐大家阅读这本书
|