/**
* 当一个对象有多个可选构造参数,可以选择builder模式构造对象,
* 通过有意义的函数名称来避免构造对象的时参数犯迷糊
* @author orange
* @description
*/
public class Person {
private String name;
private int age;
private String address;
private String telNum;
private boolean isMarriage;
private String company;
public static class Builder{
//required
private final String name;
private final int age;
//optional
private String address="";
private String telNum="";
private boolean isMarriage=false;
private String company="";
public Builder (String name,int age){
this.name=name;
this.age=age;
}
public Builder address(String val){
address=val;
return this;
}
public Builder telNum(String val){
telNum=val;
return this;
}
public Builder isMarriage(boolean val){
isMarriage=val;
return this;
}
public Builder company(String val){
company=val;
return this;
}
public Person build(){
return new Person(this);
}
}
private Person(Builder builder){
this.name=builder.name;
this.age=builder.age;
this.address=builder.address;
this.telNum=builder.telNum;
this.isMarriage=builder.isMarriage;
this.company=builder.company;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", telNum='" + telNum + '\'' +
", isMarriage=" + isMarriage +
", company='" + company + '\'' +
'}';
}
}