建造者模式的定义:
建造者模式是把产品跟产品建造过程进行解耦,又称作生成器模式。可以将复杂对象的建造过程、流程抽象出来,使这个抽象过程的不同实现方法可以构造出不同表现的对象。
我们不需要知道内部的具体构造细节。
简单小demo:
import com.sun.org.apache.xerces.internal.impl.dv.xs.QNameDV;
import org.apache.flink.table.planner.expressions.E;
import org.apache.flink.table.planner.expressions.In;
import java.io.BufferedReader;
public class Person {
private int age;
private String name;
public Person(Builder builder) {
this.age = builder.age;
this.name = builder.name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setQNameDV(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", QNameDV='" + name + '\'' +
'}';
}
public static class Builder {
private int age;
private String name;
public Builder setage(int age) {
this.age = age;
return this;
}
public Builder setQName(String name) {
this.name = name;
return this;
}
public Person build() {
return new Person(this);
}
}
public static void main(String[] args) {
Builder builder = new Builder();
builder.setage(18);
Person build = builder.build();
System.out.println(build.toString());
}
}