为什么使用builder模式
如果一个类有多个成员变量,那么我们创建该对象并为成员变量赋值的时候,代码将会变的很复杂。
比如一个Person1类,有id,name, age, address等四个属性(或者更多),一般我们的写法都会是
public class Person1 {
private int id;
private String name;
private int age;
private String address;
public void setId(int id) {
this.id = id;
}
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 static void main(String[] args) {
Person1 person1 = new Person1();
person1.setId(1);
person1.setName("xiaoming");
person1.setAge(10);
person1.setAddress("beijing");
}
}
这样我们就创建一个类,但是这种方式很容易导致不一致性,比如我们一个线程执行到person1.setAge(10);另外一个线程以为已经执行好了就拿去使用,导致获取地址是空的。
如果有大量的属性还有一种写法就是:
public class Person2 {
private int id;
private String name;
private int age;
private String address;
public Person2(int id, String name){
this(id,name,0);
}
public Person2(int id, String name, int age){
this(id,name,age,"");
}
public Person2(int id, String name, int age, String address){
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public static void main(String[] args) {
Person2 p = new Person2(1 , "xiaoming");
System.out.println(p.toString());
}
@Override
public String toString() {
return "Person2{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
这种写法也能新建一个对象,但是这种方式存在的缺点就是如果有大量的属性需要赋值,会出现赋值顺序造成混乱,或者对于不想赋值的属性,也给了设定的默认值。为了解决上述问题,就可以使用builder模式了。
builder模式
代码如下
public class person {
private final int id;
private final String name;
private final int age;
private final String address;
private person(Builder builder){
id = builder.getId();
name = builder.getName();
age = builder.getAge();
address = builder.getAddress();
}
public static class Builder{
private final int id;
private final String name;
private int age;
private String address;
//必填项可以放在构造函数里
public Builder(int id,String name){
this.id = id;
this.name = name;
}
public Builder setId(int id) {
return this;
}
public Builder setAge(int age) {
this.age = age;
return this;
}
public Builder setAddress(String address) {
this.address = address;
return this;
}
public person build(){
return new person(this);
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public static void main(String[] args) {
person p = new person.Builder(1, "xiaoming").setAge(10).setAddress("beijing").build();
System.out.println(p.toString());
}
@Override
public String toString() {
return "person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
这样就很好的解决上述问题。
小结
Builder模式通常作为配置类的构建器将配置的构建和表示分离开来,同时也是将配置从目标类中隔离出来,避免作为过多的setter方法。Builder模式比较常见的实现形式是通过链式调用,这样使得代码更加简洁、易懂。
优点:
1.良好的封装性,使用建造者模式可以使客户端不必知道产品内部组成的细节
2.建造者独立,容易扩展
缺点:
会产生多余的Builder对象以及Director对象,消耗内存。