/**
* 建造者
*/
public abstract class Builder {
public abstract Builder buildA(String s);
public abstract Builder buildB(String s);
public abstract Builder buildC(String s);
public abstract Product build();
}
public class ConcreteBuilder extends Builder {
private Product product;
public ConcreteBuilder() {
product = new Product();
}
@Override
public Builder buildA(String s) {
product.setBuildA(s);
return this;
}
@Override
public Builder buildB(String s) {
product.setBuildB(s);
return this;
}
@Override
public Builder buildC(String s) {
product.setBuildC(s);
return this;
}
@Override
public Product build() {
return product;
}
}
public class Product {
private String buildA="汉堡";
private String buildB="饮料";
private String buildC="薯条";
public String getBuildA() {
return buildA;
}
public void setBuildA(String buildA) {
this.buildA = buildA;
}
public String getBuildB() {
return buildB;
}
public void setBuildB(String buildB) {
this.buildB = buildB;
}
public String getBuildC() {
return buildC;
}
public void setBuildC(String buildC) {
this.buildC = buildC;
}
@Override
public String toString() {
return "Product2{" +
"buildA='" + buildA + '\'' +
", buildB='" + buildB + '\'' +
", buildC='" + buildC + '\'' +
'}';
}
}
public class TestProduct {
public static void main(String[] args) {
ConcreteBuilder concreteBuilder = new ConcreteBuilder();
Product build = concreteBuilder.buildA("aa").buildB("bb").buildC("cc").build();
// Product{buildA='aa', buildB='bb', buildC='cc'}
System.out.println(build);
}
}