Builder模式是一步一步创建一个复杂的对象的创建型模式.它将对象的构建与表示分离.
public class Person{
private int age;
private String name;
private Person(Builder builder){
age = builder.age;
name = build.name;
}
class Builder{
int age;
String name;
public Builder setAge(int age){
this.age = age;
return Builder;
}
public Builder setName(String name){
this.name = name;
return Builder;
}
public Person build(){
return new Person(this);
}
}
}
public class Client{
public static void main(String[] args){
Person person = new Person.Builder().setAge(18).setName(" ").build();
}
}