爲什麼使用建造者模式
代碼中,對象是對現實世界物的抽象。一個對象可以含有大量的必要、不必要的屬性,我們在創建實例時,若採用帶參構 造器、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='未設置'}
這種方式最大的好處就是在創建中使用流式創建對象,控制實例變量時需要的屬性。但是,在構建這個類時代碼會很龐大。