构造器模式用于提供灵活的对象构造方法, 解决 复杂对象的构造问题.
本文总结了平时工作中使用到的三种构造模式的实现. 具体逻辑没什么难的, 直接上代码:
纯代码方式
ENTITY
/**
* 逻辑表描述符
*
* @author: LiKe
* @date: 2019-07-08 19:10
*/
public final class LogicalTableDescriptor {
/**
* 逻辑表名
*/
private String tableName;
/**
* 逻辑列名
*/
private final List<String> columnNameList = new ArrayList<>();
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public List<String> getColumnNameList() {
return columnNameList;
}
public void setColumnName(String columnName) {
this.columnNameList.add(columnName);
}
}
BUILDER
/**
* LogicalTableDescriptor 构建器
*
* @author: LiKe
* @date: 2019-07-08 19:13
*/
public final class LogicalTableDescriptorBuilder {
/**
* 被构建的实例的引用
*/
private final LogicalTableDescriptor descriptor = new LogicalTableDescriptor();
private LogicalTableDescriptorBuilder() {
}
/**
* Description: 构建一个实例
*
* @return cn.speedit.finance_server.descriptor.builder.LogicalTableDescriptorBuilder
* @author LiKe
* @date 2019-07-08 19:16:09
*/
public static LogicalTableDescriptorBuilder of() {
return new LogicalTableDescriptorBuilder();
}
/**
* Description: 构建逻辑表名
*
* @param tableName 逻辑表名
* @return cn.speedit.finance_server.descriptor.builder.LogicalTableDescriptorBuilder
* @author LiKe
* @date 2019-07-08 19:18:56
*/
public LogicalTableDescriptorBuilder tableName(String tableName) {
descriptor.setTableName(tableName);
return this;
}
/**
* Description: 构建逻辑列名
*
* @param columnName 逻辑列名
* @return cn.speedit.finance_server.descriptor.builder.LogicalTableDescriptorBuilder
* @author LiKe
* @date 2019-07-08 19:19:46
*/
public LogicalTableDescriptorBuilder columnName(String columnName) {
descriptor.setColumnName(columnName);
return this;
}
/**
* Description: 执行构建
*
* @return cn.speedit.finance_server.descriptor.LogicalTableDescriptor
* @author LiKe
* @date 2019-07-08 19:17:50
*/
public LogicalTableDescriptor build() {
return descriptor;
}
}
使用方式
LogicalTableDescriptorBuilder.of()
.tableName("table-name").columnName("column-name")
.build()
基于 JAVA 8 Comsumer 实现
这种方式代码更加简洁
ENTITY
@SuppressWarnings("unused")
public class Person {
private String firstName;
private String middleName;
private String lastName;
public Person(String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", middleName='" + middleName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
BUILDER
目标实体需要提供一个全参构建函数; Builder 需要持有目标实体的全部属性, 用其自身去构建目标实体.
public class PersonBuilder {
public String firstName;
public String middleName;
public String lastName;
public static PersonBuilder of() {
return new PersonBuilder();
}
public PersonBuilder with(Consumer<PersonBuilder> builderConsumer) {
builderConsumer.accept(this);
return this;
}
public Person create() {
return new Person(firstName, middleName, lastName);
}
private PersonBuilder() {}
}
使用方式
Person person = PersonBuilder.of()
.with(builder -> {
builder.firstName = "Li";
builder.lastName = "Ke";
})
.create();
借助 Lombok 实现
当然少不了 Lombok… 本质是通过 Java source-level annotation processing (javax.annotation.processing.*
) 技术新建了一个 Builder… 具体以后写篇文章总结一下.
ENTITY
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String username;
private String password;
private String level;
}
使用方式
User.builder().username("LiKe").password("LiKe").build().toString()
总结
个人感觉 JAVA 8 Consumer 的方式和 Lombok 的方式都有其应用场景.
Lombok 不用说, 自然是代码量最小的方式, 但是 JAVA 8 Consumer 的方式可以在构造器中对实体本身做一些自定义的验证或者转换, 更灵活一些.
~ END ~