设计模式系列之建造者模式(Build Pattern)构建实体类
模式定义
建造者模式属于23种设计模式中的创建型模式,可以理解为创建对象的一种很好的方法。
所谓建造者模式就是**将组件和组件的组件过程分开,然后一步一步建造一个复杂的对象。**所以建造者模式又叫生成器模式。 建造者模式类图
模式结构
建造者模式包含如下角色 Builder:抽象建造者 ConcreteBuilder:具体建造者 Director:指挥者 Product:产品角色
如果系统只需要一个具体的建造者类的时候,可以省略抽象建造者,有时候指挥者类也可以省略,让建造者类同时充当指挥者和建造者
例子实践
代码语言:javascript复制package com.extra.licenseLog.model;
import java.util.Date;
/**
* <pre>
* 证照操作记录
* </pre>
*
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/08/15 10:21 修改内容:
* </pre>
*/
public class LicenseLogModel {
/**
* 日志SEQ
*/
private String seq;
/**
* 痕迹表SEQ
*/
private String handleSeq;
/**
* 查验证照用户账号
*/
private String userCode;
/**
* 查验证照编码
*/
private String certCode;
/**
* 查验证照时间
*/
private Date certTime;
/**
* 办件流水号
*/
private String controlSeq;;
public LicenseLogModel() {
}
public LicenseLogModel(String seq) {
this.seq = seq;
}
public LicenseLogModel(String seq, String handleSeq, String userCode, String certCode, Date certTime, String controlSeq) {
this.seq = seq;
this.handleSeq = handleSeq;
this.userCode = userCode;
this.certCode = certCode;
this.certTime = certTime;
this.controlSeq = controlSeq;
}
private LicenseLogModel(Builder builder){
this.seq = builder.seq;
this.handleSeq = builder.handleSeq;
this.userCode = builder.userCode;
this.certCode = builder.certCode;
this.certTime = builder.certTime;
this.controlSeq = builder.controlSeq;
}
public String getSeq() {
return seq;
}
public void setSeq(String seq) {
this.seq = seq;
}
public String getHandleSeq() {
return handleSeq;
}
public void setHandleSeq(String handleSeq) {
this.handleSeq = handleSeq;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public Date getCertTime() {
return certTime;
}
public void setCertTime(Date certTime) {
this.certTime = certTime;
}
public String getCertCode() {
return certCode;
}
public void setCertCode(String certCode) {
this.certCode = certCode;
}
public String getControlSeq() {
return controlSeq;
}
public void setControlSeq(String controlSeq) {
this.controlSeq = controlSeq;
}
public static class Builder {
private String seq;
private String handleSeq;
private String userCode;
private String certCode;
private Date certTime;
private String controlSeq;
public Builder(String seq){
this.seq = seq;
}
public Builder setSeq(String seq) {
this.seq = seq;
return this;
}
public Builder setHandleSeq(String handleSeq) {
this.handleSeq = handleSeq;
return this;
}
public Builder setUserCode(String userCode) {
this.userCode = userCode;
return this;
}
public Builder setCertCode(String certCode){
this.certCode = certCode;
return this;
}
public Builder setCertTime(Date certTime) {
this.certTime = certTime;
return this;
}
public Builder setControlSeq(String controlSeq){
this.controlSeq = controlSeq;
return this;
}
public LicenseLogModel build(){
return new LicenseLogModel(this);
}
}
@Override
public String toString() {
return "LicenseLogModel{"
"seq='" seq '''
", handleSeq='" handleSeq '''
", userCode='" userCode '''
", certCode='" certCode '''
", certTime=" certTime
", controlSeq='" controlSeq '''
'}';
}
}
代码语言:javascript复制LicenseLogModel logModel = new LicenseLogModel.Builder(commonService.generateUUID())
.setHandleSeq(controlStuff.getHandleSeq())
.setUserCode(userCode)
.setCertCode(authCode)
.setCertTime(new Date(System.currentTimeMillis()))
.setControlSeq(controlStuff.getControlSeq())
.build();
参考同事写的工具类:
代码语言:javascript复制package com.template.utils;
import java.util.HashMap;
import java.util.Map;
/**
* 信息工具类
* @date 2018-05-22
*/
public class Maps {
/**
* 成功状态值
*/
public static final String SUCCESS_VALUE = "1";
/**
* 失败状态值
*/
public static final String ERROR_VALUE = "0";
/**
* 信息key
*/
public static final String MESSAGE_KEY = "message";
/**
* 状态key
*/
public static final String STATE_KEY = "status";
/**
* 数据key
*/
public static final String DATA_KEY = "data";
private Maps(){}
/**
* 获取信息MapMessage实例
* @return
*/
public static MapMessageBulider MapMessageBulider(){
return new MapMessageBulider();
}
/**
* 内部类:信息
* @author csy
* @date 2018-05-22
*/
public static class MapMessageBulider{
private Map<String, Object> innerMap = new HashMap<String, Object>();
/**
* 自由封装key/value
* @param key
* @param value
* @return
*/
public MapMessageBulider setKeyValue(String key,Object value){
this.innerMap.put(key, value);
return this;
}
/**
* 返回map
* @return
*/
public Map<String, Object> bulid(){
return this.innerMap;
}
/**
* 设置成功返回值及信息
* 已设置默认的success/message/data key
* @param data 数据
* @param message 成功信息
* @return
*/
public Map<String, Object> setSuccessBulid(Object data,Object message){
this.setKeyValue(STATE_KEY, SUCCESS_VALUE)
.setKeyValue(MESSAGE_KEY, message)
.setKeyValue(DATA_KEY, data);
return this.bulid();
}
/**
* 设置返回失败数据及信息
* 已设置默认的success/message/data key
* @param data 数据
* @param message 失败信息
* @return
*/
public Map<String, Object> setErrorBulid(Object data,Object message){
this.setKeyValue(STATE_KEY, ERROR_VALUE)
.setKeyValue(MESSAGE_KEY, message)
.setKeyValue(DATA_KEY, data);
return this.bulid();
}
}
}