面向接口编程-应用审批
作者:互联网
需求如上,上次已经 使用策略模式 和工厂将审批的动作实现了:
public enum AuditActionEnum {
commit("commit", "提交审核"),
approvePass("approvePass", "审核通过"),
approveReject("approveReject", "审核拒绝");
private String key;
private String desc;
AuditActionEnum(String key, String desc) {
this.key = key;
this.desc = desc;
}
public String getKey() {
return this.key;
}
public String getDesc() {
return this.desc;
}
public static AuditActionEnum match(String key) {
for (AuditActionEnum e : AuditActionEnum.values()) {
if (e.getKey().equals(key)) {
return e;
}
}
return null;
}
}
@Component
public interface IAuditStrategy {
void doAction(IService service , Long id , AuditCommitResDto auditCommitResDto);
}
具体实现子类:
@Component("commit")
public class CommitAuditStrategy implements IAuditStrategy {
@Override
public void doAction(IService service, Long id, AuditCommitResDto auditCommitResDto) {
service.update(new UpdateWrapper<>()
.set("audit_id", auditCommitResDto.getAuditId())
.set("commit_time", auditCommitResDto.getCommitTime())
.set("audit_state", auditCommitResDto.getAuditState())
.eq("id", id)
);
}
}
@Component("approvePass")
public class ApprovePassAuditStrategy implements IAuditStrategy {
@Override
public void doAction(IService service, Long id, AuditCommitResDto auditCommitResDto) {
//审核通过先把之前数据失效 ,在把通过的这条生效
service.update(new UpdateWrapper<>()
.set("is_effective", EffectMasterEnum.effect.getKey())
.eq("master_id", id)
);
service.update(new UpdateWrapper<>()
.set("is_effective", EffectMasterEnum.effect.getKey())
.set("audit_time", auditCommitResDto.getAuditTime())
.set("audit_state", auditCommitResDto.getAuditState())
.eq("id", id)
);
}
}
再仔细看看,发现其实做的事情都是同样的:即 对
is_effective 生效
audit_time 审批时间
audit_state 审批状态
这三个字段进行更新。
例子:假如对商品进行审批 (用 @Data 省略了 set get 方法)
@Data
ProductEntity {
id
productName
isEffective
auditTime
auditState
}
对销量进行审批:
@Data
SellEntity {
id
count
isEffective
auditTime
auditState
}
通过观察不难发现 :只要 需要进行审批 就必须 有
is_effective 生效
audit_time 审批时间
audit_state 审批状态
这三个字段,于是想通过 将三个字段提取出来为一个类:
@Data
AuditEntity {
isEffective
auditTime
auditState
}
@Data
ProductEntity extends AuditEntity {
id
productName
}
对销量进行审批:
@Data
SellEntity extends AuditEntity
{
id
count
}
然后 对策略开始修改:
@Component("commit")
public class CommitAuditStrategy implements IAuditStrategy {
@Override
public void doAction(Audit audit , AuditCommitResDto auditCommitResDto) {
audit.setAuditTime(auditCommitResDto.getCommitTime())
audit.setAuditState(auditCommitResDto.getAuditState())
);
}
}
调用:
如果是商品审批
ProductEntity product = new ProductEntity
auditCommitResDto = 调接口返回
auditStrategyMap.get(dto.getAuditAction()).doAction(product ,auditCommitResDto);
this.update (product )
SellEntity sell= new SellEntity
auditCommitResDto = 调接口返回
auditStrategyMap.get(dto.getAuditAction()).doAction(sell,auditCommitResDto);
this.update (sell)
到这里看起就已经将需求实现了,但仍然有问题。 使用 继承不易于拓展 ,并且entity,java 中不允许使用多继承,所以组长希望叫我改成接口方式,于是改将audit 改成接口:
interface IAuditEntity {
void setIsEffective(Integer isEffective);
void setAuditState(String auditState);
void setAuditTime(Date auditTime);
}
@Data
ProductEntity implements IAuditEntity {
id
productName
isEffective
auditTime
auditState
}
对销量进行审批:
@Data
SellEntity implements IAuditEntity {
id
count
isEffective
auditTime
auditState
}
然后 对策略开始修改:
@Component("commit")
public class CommitAuditStrategy implements IAuditStrategy {
@Override
public void doAction(IAuditEntity audit , AuditCommitResDto auditCommitResDto) {
audit.setAuditTime(auditCommitResDto.getCommitTime())
audit.setAuditState(auditCommitResDto.getAuditState())
);
}
}
调用:
如果是商品审批
ProductEntity product = new ProductEntity
auditCommitResDto = 调接口返回
auditStrategyMap.get(dto.getAuditAction()).doAction(product ,auditCommitResDto);
this.update (product )
SellEntity sell= new SellEntity
auditCommitResDto = 调接口返回
auditStrategyMap.get(dto.getAuditAction()).doAction(sell,auditCommitResDto);
this.update (sell)
用接口 还有一个好处,其一是接口可以多实现 ,更易于拓展。其二 只要涉及审批相关的实体,倒逼着开发一定要有审批相关的几个字段。(因为entity 用mybatits plus 绑定了数据库表字段 所以表设计的时候就不会漏字段)
这时才想起来 接口的意义:定义规范。 并且这体现java语言的多态,
IAuditEntity product= new ProductEntity
IAuditEntity product= new SellEntity
这里和 List list = new ArrayList() List list = new LinkList() 一样。
标签:audit,doAction,编程,接口,auditCommitResDto,public,面向,new,id 来源: https://www.cnblogs.com/publicmain/p/16245271.html