建造者模式实例
在我们平时编码过程中如果遇到一些特别复杂的Model,或者Model中各个字段之间存在一些依赖关系的时候,为了避免逻辑混乱,可以使用建造者模式进行封装处理。
首先建造者模式可以使用如下方法
package 建造者模式.v1;
public class BuildTest {
public static void main(String[] args) {
// Product product = new Product();
// product.setCompanyName("xxx");
// product.setPart1("xxx");
// //......
ProductBuilder productBuilder = new DefaultConcreteProductBuilder();
Director director = new Director(productBuilder);
Product product = director.makeProduct("companyName", "part1", "part2", "part3", "part4", "part5");
System.out.println(product);
}
}
interface ProductBuilder{
void builderCompanyName(String companyName);
void builderPart1(String part1);
void builderPart2(String part2);
void builderPart3(String part3);
void builderPart4(String part4);
void builderPart5(String part5);
Product build();
}
class DefaultConcreteProductBuilder implements ProductBuilder{
private String companyName;
private String part1;
private String part2;
private String part3;
private String part4;
private String part5;
@Override
public void builderCompanyName(String companyName) {
this.companyName = companyName;
}
@Override
public void builderPart1(String part1) {
this.part1 = part1;
}
@Override
public void builderPart2(String part2) {
this.part2 = part2;
}
@Override
public void builderPart3(String part3) {
this.part3 = part3;
}
@Override
public void builderPart4(String part4) {
this.part4 = part4;
}
@Override
public void builderPart5(String part5) {
this.part5 = part5;
}
@Override
public Product build() {
return new Product(this.companyName,this.part1,this.part2,this.part3,this.part4,this.part5);
}
}
class Director{
private ProductBuilder builder;
public Director(ProductBuilder builder){
this.builder=builder;
}
public Product makeProduct(String companyName, String part1, String part2, String part3, String part4, String part5){
builder.builderCompanyName(companyName);
builder.builderPart1(part1);
builder.builderPart2(part2);
builder.builderPart3(part3);
builder.builderPart4(part4);
builder.builderPart5(part5);
Product product = builder.build();
return product;
}
}
class Product {
private String companyName;
private String part1;
private String part2;
private String part3;
private String part4;
private String part5;
//......
public Product() {
}
public Product(String companyName, String part1, String part2, String part3, String part4, String part5) {
this.companyName = companyName;
this.part1 = part1;
this.part2 = part2;
this.part3 = part3;
this.part4 = part4;
this.part5 = part5;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getPart1() {
return part1;
}
public void setPart1(String part1) {
this.part1 = part1;
}
public String getPart2() {
return part2;
}
public void setPart2(String part2) {
this.part2 = part2;
}
public String getPart3() {
return part3;
}
public void setPart3(String part3) {
this.part3 = part3;
}
public String getPart4() {
return part4;
}
public void setPart4(String part4) {
this.part4 = part4;
}
public String getPart5() {
return part5;
}
public void setPart5(String part5) {
this.part5 = part5;
}
@Override
public String toString() {
return "Product{" +
"companyName='" + companyName + '\'' +
", part1='" + part1 + '\'' +
", part2='" + part2 + '\'' +
", part3='" + part3 + '\'' +
", part4='" + part4 + '\'' +
", part5='" + part5 + '\'' +
'}';
}
}
使用ProductBuilder定义接口,实现类的构建,使用Director来定义构建方式和构建顺序,以此达到定制化的构建。
如果业务没有上述那么复杂,也可以使用以下方式动态的配置类的构建过程。
public class BuildTest {
public static void main(String[] args) {
Product.Builder builder = new Product.Builder().productName("productName").part1("part1").part2("part2").part3("part3").part4("part4").part5("part5");
Product product = builder.build();
System.out.println(product);
}
}
class Product {
private final String companyName;
private final String part1;
private final String part2;
private final String part3;
private final String part4;
private final String part5;
//......
public Product(String companyName, String part1, String part2, String part3, String part4, String part5) {
this.companyName = companyName;
this.part1 = part1;
this.part2 = part2;
this.part3 = part3;
this.part4 = part4;
this.part5 = part5;
}
static class Builder{
private String companyName;
private String part1;
private String part2;
private String part3;
private String part4;
private String part5;
public Builder productName(String productName){
this.companyName=productName;
return this;
}
public Builder part1(String part1){
this.part1=part1;
return this;
}
public Builder part2(String part2){
this.part2=part2;
return this;
}
public Builder part3(String part3){
this.part3=part3;
return this;
}
public Builder part4(String part4){
this.part4=part4;
return this;
}
public Builder part5(String part5){
this.part5=part5;
return this;
}
Product build(){
Product product = new Product(this.companyName, this.part1, this.part2, this.part3, this.part4, this.part5);
return product;
}
}
@Override
public String toString() {
return "Product{" +
"companyName='" + companyName + '\'' +
", part1='" + part1 + '\'' +
", part2='" + part2 + '\'' +
", part3='" + part3 + '\'' +
", part4='" + part4 + '\'' +
", part5='" + part5 + '\'' +
'}';
}
}
可以使用静态内部类Builder构建,每次赋值以后返回当前对象,以此达到可以链式动态赋值的方式。
应用场景
- 需要生成的对象具有复杂的内部结构。
- 需要生成的对象内部属性本身相互依赖。
- 与不可变对象(final)配合使用。
优点
- 建造者独立,以扩展。
- 便于控制细节风险。