为什么使用建造者模式
代码中,对象是对现实世界物的抽象。一个对象可以含有大量的必要、不必要的属性,我们在创建实例时,若采用带参构 造器、getter/setter方法,由于参数过多,很容易引起混乱。 这时,引入构建者模式,可以清晰的观测到实例化过程中,需要设置的属性值。
什么是建造者模式
建造者模式(Builder Pattern) 又名生成器模式,是一种对象构建模式。它可以将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。
建造者模式的实现
直接上代码:
package builder.source.pojo;
/**
* @author hw
* @createTime 2018/9/4
* @dscrb
*/
public class Person {
//必写信息
private String name;
private String phoneNumber;
//可写可不写
private Integer age;
private char sex;
private String address;
private Integer height;
private Integer weight;
private String job;
private String salary;
private String hobby;
private Person(){
return
}
private Person(Builder builder){
this.name = builder.name;
this.phoneNumber = builder.phoneNumber;
this.age = builder.age;
this.sex = builder.sex;
this.address = builder.address;
this.height = builder.height;
this.weight = builder.weight;
this.job = builder.job;
this.salary = builder.salary;
this.hobby = builder.hobby;
}
public static class Builder{
private String name;
private String phoneNumber;
private Integer age;
private char sex;
private String address ;
private Integer height ;
private Integer weight ;
private String job ;
private String salary ;
private String hobby;
public Builder(){
this.age = 0;
this.address = "未设置";
this.height = 0;
this.weight = 0;
this.job = "未设置";
this.salary = "未设置";
this.hobby = "未设置";
}
public Builder(String name, String phoneNumber){
this.name = name;
this.phoneNumber = phoneNumber;
this.age = 0;
this.address = "未设置";
this.height = 0;
this.weight = 0;
this.job = "未设置";
this.salary = "未设置";
this.hobby = "未设置";
}
public Person build(){
return new Person(this);
}
public Builder name(String name){
this.name = name;
return this;
}
public Builder phoneNumber(String phoneNumber){
this.phoneNumber = phoneNumber;
return this;
}
public Builder age(Integer age){
this.age = age;
return this;
}
public Builder sex(char sex){
this.sex = sex;
return this;
}
public Builder address(String address){
this.address = address;
return this;
}
public Builder height(Integer height){
this.height = height;
return this;
}
public Builder weight(Integer weight){
this.weight = weight;
return this;
}
public Builder job(String job){
this.job = job;
return this;
}
public Builder salary(String salary){
this.salary = salary;
return this;
}
public Builder hobby(String hobby){
this.hobby = hobby;
return this;
}
}
//省略Person类中的getter/setter方法
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
", age=" + age +
", sex=" + sex +
", address='" + address + '\'' +
", height=" + height +
", weight=" + weight +
", job='" + job + '\'' +
", salary='" + salary + '\'' +
", hobby='" + hobby + '\'' +
'}';
}
}
测试代码:
package builder.source.test;
import builder.source.pojo.Person;
/**
* @author hw
* @createTime 2018/9/4
* @dscrb
*/
public class SetPerson {
public static void main(String[] args) {
set();
}
public static void set(){
Person person1 = new Person.Builder().name("小小").age(23).build();
System.out.println(person1);
person1.setHobby("看书");
System.out.println(person1);
Person person2 = new Person.Builder().age(23).address("高新").height(170).hobby("听歌").job("IT").salary("111").sex('m').weight(50).build();
System.out.println(person2);
Person person3 = new Person.Builder("小胡","13227012222").build();
System.out.println(person3);
}
}
测试结果:
Person{name='小小', phoneNumber='null', age=23, sex= , address='未设置', height=0, weight=0, job='未设置', salary='未设置', hobby='未设置'}
Person{name='小小', phoneNumber='null', age=23, sex= , address='未设置', height=0, weight=0, job='未设置', salary='未设置', hobby='看书'}
Person{name='null', phoneNumber='null', age=23, sex=m, address='高新', height=170, weight=50, job='IT', salary='111', hobby='听歌'}
Person{name='小胡', phoneNumber='13227012222', age=0, sex= , address='未设置', height=0, weight=0, job='未设置', salary='未设置', hobby='未设置'}
这种方式最大的好处就是在创建中使用流式创建对象,控制实例变量时需要的属性。但是,在构建这个类时代码会很庞大。