目录
1 Builder模式
Buidler模式主要分为两种:1、经典Builder模式;2、链式变形Builder模式。
当一个类的构造函数参数个数超过4个,而且这些参数有些是可选的参数,考虑使用构造者模式(这是一个重要的应用场景:必选参数和可选参数)。
2 经典Builder模式
2.1 抽象建造者角色(Builder)
创建一个Product对象的各个部件指定抽象接口,以规范产品对象的各个组成成分的建造。一般而言,此角色规定要实现复杂对象的哪些部分的创建,并不涉及具体的对象部件的创建。
/**
* 行高列宽信息(经典Builder模式-抽象Builder)
*
* @author xudongmaster
*/
public abstract class Builder {
abstract RowHeightColWidthModel build();
}
2.2 具体建造者(ConcreteBuilder)
1)实现Builder的接口以构造和装配该产品的各个部件。即实现抽象建造者角色Builder的方法。
2)定义并明确它所创建的表示,即针对不同的商业逻辑,具体化复杂对象的各部分的创建。
3) 提供一个检索产品的接口。
4) 构造一个使用Builder接口的对象即在指导者的调用下创建产品实例。
package com.xudongbase.designpattern.builder.classic;
import lombok.Getter;
/**
* 行高列宽信息(经典Builder模式-具体Builder)
*
* @author xudongmaster
*/
@Getter
public class RowHeightColWidthModelBuilder extends Builder {
/**
* sheet名称
*/
private String sheetName;
/**
* 行号
*/
private Integer rowIndex;
/**
* 列号
*/
private Integer colIndex;
/**
* 行高
*/
private Float rowHeight;
/**
* 列宽
*/
private Integer colWidth;
protected void rowIndex(Integer rowIndex) {
this.rowIndex = rowIndex;
}
protected void colIndex(Integer colIndex) {
this.colIndex = colIndex;
}
protected void rowHeight(Float rowHeight) {
this.rowHeight = rowHeight;
}
protected void colWidth(Integer colWidth) {
this.colWidth = colWidth;
}
public RowHeightColWidthModelBuilder(String sheetName) {
this.sheetName = sheetName;
}
@Override
public RowHeightColWidthModel build() {
return new RowHeightColWidthModel(this);
}
}
2.3 指导者(Director)
调用具体建造者角色以创建产品对象的各个部分。指导者并没有涉及具体产品类的信息,真正拥有具体产品的信息是具体建造者对象。它只负责保证对象各部分完整创建或按某种顺序创建。
package com.xudongbase.designpattern.builder.classic;
/**
* 行高列宽信息(经典Builder模式-Director)
*
* @author xudongmaster
*/
public class RowHeightColWidthModelDirector {
private RowHeightColWidthModelBuilder builder;
public RowHeightColWidthModelDirector(RowHeightColWidthModelBuilder builder) {
this.builder = builder;
}
public void construct(Integer rowIndex, Float rowHeight, Integer colIndex, Integer colWidth) {
builder.rowIndex(rowIndex);
builder.rowHeight(rowHeight);
builder.colIndex(colIndex);
builder.colWidth(colWidth);
}
}
2.4 产品角色(Product)
建造中的复杂对象。它要包含那些定义组件的类,包括将这些组件装配成产品的接口。
package com.xudongbase.designpattern.builder.classic;
import lombok.Getter;
/**
* 行高列宽信息(经典Builder模式-Product)
*
* @author xudongmaster
*/
@Getter
public class RowHeightColWidthModel {
/**
* sheet名称
*/
private String sheetName;
/**
* 行号
*/
private Integer rowIndex;
/**
* 列号
*/
private Integer colIndex;
/**
* 行高
*/
private Float rowHeight;
/**
* 列宽
*/
private Integer colWidth;
public RowHeightColWidthModel(RowHeightColWidthModelBuilder builder) {
this.sheetName = builder.getSheetName();
this.rowIndex = builder.getRowIndex();
this.colIndex = builder.getColIndex();
this.rowHeight = builder.getRowHeight();
this.colWidth = builder.getColWidth();
}
}
2.5 调试代码
/**
* 测试经典Builder模式
*/
@Test
public void testClassicBuilder() {
String sheetName = "sheet1";
//生成Builder
RowHeightColWidthModelBuilder builder = new RowHeightColWidthModelBuilder(sheetName);
//Director装配Builder
RowHeightColWidthModelDirector director = new RowHeightColWidthModelDirector(builder);
director.construct(1, 20f, 2, 30);
//Builder生成product
RowHeightColWidthModel model = builder.build();
}
3 链式Builder模式
3.1 产品角色(Product)
Product类内部集成一个静态内部类Builder,通过Builder类建造Product类实例。而且Builder类赋值可以通过链式的方式进行赋值。
package com.xudongbase.designpattern.builder.chained;
import lombok.Getter;
/**
* 行高列宽信息(链式Builder模式)
*
* @author xudongmaster
*/
@Getter
public class RowHeightColWidthModel {
/**
* sheet名称
*/
private String sheetName;
/**
* 行号
*/
private Integer rowIndex;
/**
* 列号
*/
private Integer colIndex;
/**
* 行高
*/
private Float rowHeight;
/**
* 列宽
*/
private Integer colWidth;
private RowHeightColWidthModel(Builder builder) {
this.sheetName = builder.sheetName;
this.rowIndex = builder.rowIndex;
this.colIndex = builder.colIndex;
this.rowHeight = builder.rowHeight;
this.colWidth = builder.colWidth;
}
public static class Builder {
/**
* sheet名称
*/
private String sheetName;
/**
* 行号
*/
private Integer rowIndex;
/**
* 列号
*/
private Integer colIndex;
/**
* 行高
*/
private Float rowHeight;
/**
* 列宽
*/
private Integer colWidth;
public Builder rowIndex(Integer rowIndex) {
this.rowIndex = rowIndex;
return this;
}
public Builder colIndex(Integer colIndex) {
this.colIndex = colIndex;
return this;
}
public Builder rowHeight(Float rowHeight) {
this.rowHeight = rowHeight;
return this;
}
public Builder colWidth(Integer colWidth) {
this.colWidth = colWidth;
return this;
}
public Builder(String sheetName) {
this.sheetName = sheetName;
}
public RowHeightColWidthModel build() {
return new RowHeightColWidthModel(this);
}
}
}
3.2 调试代码
/**
* 测试链式Builder模式
*/
@Test
public void testChainedBuilder() {
String sheetName = "sheet1";
//product内部的Builder生成product
com.xudongbase.designpattern.builder.chained.RowHeightColWidthModel model = new com.xudongbase.designpattern.builder.chained.RowHeightColWidthModel.Builder(sheetName)
.rowIndex(1).rowHeight(20f).colIndex(2).colWidth(30).build();
}
注:
需要查看源码请前往Gitee的xudongbase的design_pattern分支。