最近在实现一个简单的审核系统,包含多个审核状态和多个审核动作,将业务逻辑映射到代码中感觉会有很多繁杂的逻辑,因此,会思考用一个比较通用、易扩展、易维护、易测试的模式来应对这种场景,了解了一下,就是状态模式。笔者将业务场景简单化,并通过状态模式来解决这个问题,业务场景如下:
几点说明:
1.用户提价资料后,为初始状态, 可以审核通过,也可以审核拒绝;
2.审核通过后状态不可以改变;
3.审核拒绝后可以重新发起审核;
在正式介绍之前,可以思考一下,如何实现这种场景?如果状态比较多,流程复杂,如何解决?
一、核心概念
状态机有三部分组成:
1)状态(State):整个业务流转过程中所有的状态,对于本例来说有三个状态:初始、审核通过、审核拒绝;
2)事件(Event):转移条件(Transition Condition),事件的触发会导致状态的转移以及相应的动作;
3)动作(Action):事件转移过程对对应的时间,包括发邮件,更新数据库,调用API等等,当然相关动作未必必须执行。
二、代码框架
public enum State{} //定义整体流程中的所有状态
public enum Event{} //定义所有事件
public interface IAudit {} //定义事件执行接口类,每个method都是某个时间event对应的具体指定动作
public abstract class AbstractAudit implements IAudit {} //事件执行抽象类
public class StateAudit extends AbstractAudit implements IAudit {} //具体状态类,method为当前状态下对应event执行的具体事情。
public class AuditStateMachine {} //对外业务类
三、业务实现
State.java:
package design.pattern.statepattern.test;
public enum State {
audit_init("audit_init"),
audit_approved("audit_approved"),
audit_rejected("audit_rejected");
private String value;
private State(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
}
Event.java:
package design.pattern.statepattern.test;
public enum Event {
APPROVE("approve"),
REJECT("reject"),
GOBACKINIT("gobackinit");
private String event;
private Event(String event) {
this.event = event; }
public String getEvent() {
return this.event;
}
}
IAudit.java:
public interface IAudit {
void triggerEvent(Event e);
String auditApproved();
String auditReject();
String goBackToReview();
}
AbstractAudit.java:
package design.pattern.statepattern.test;
public abstract class AbstractAudit implements IAudit {
//todo: use a table mapping
@Override
public void triggerEvent(Event e) {
if (e.getEvent() == Event.APPROVE.getEvent()) {
auditApproved();
} else if (e.getEvent() == Event.REJECT.getEvent()) {
auditReject();
} else if (e.getEvent() == Event.GOBACKINIT.getEvent()) {
goBackToReview();
} else {
System.out.println("invalid event = " + e.getEvent());
}
}
@Override
public String auditApproved() {
System.out.println("auditApproved-N/A");
return "N/A";
}
@Override
public String auditReject() {
System.out.println("auditReject-N/A");
return "N/A";
}
@Override
public String goBackToReview() {
System.out.println("goBackToReview-N/A");
return "N/A";
}
}
ApprovedAudit.java:
public class ApprovedAudit extends AbstractAudit implements IAudit {}
InitAudit.java:
package design.pattern.statepattern.test.impl;
import design.pattern.statepattern.test.AbstractAudit;
import design.pattern.statepattern.test.IAudit;
import design.pattern.statepattern.test.State;
public class InitAudit extends AbstractAudit implements IAudit {
@Override
public String auditApproved() {
System.out.println("this is InitAudit-auditApproved");
return null;
}
@Override
public String auditReject() {
System.out.println("this is InitAudit-auditReject");
return null;
}
}
RejectedAudit:
package design.pattern.statepattern.test.impl;
import design.pattern.statepattern.test.AbstractAudit;
import design.pattern.statepattern.test.IAudit;
import design.pattern.statepattern.test.State;
public class RejectedAudit extends AbstractAudit implements IAudit {
@Override
public String goBackToReview() {
System.out.println("this is RejectedAudit-goBackToReview");
return null;
}
}
AuditStateMachine.java:
package design.pattern.statepattern;
import design.pattern.statepattern.test.Event;
import design.pattern.statepattern.test.IAudit;
import design.pattern.statepattern.test.State;
import design.pattern.statepattern.test.impl.ApprovedAudit;
import design.pattern.statepattern.test.impl.InitAudit;
import design.pattern.statepattern.test.impl.RejectedAudit;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class AuditStateMachine {
protected static Map<State, IAudit> statusMap = new ConcurrentHashMap<>();
static {
statusMap.put(State.audit_init, new InitAudit());
statusMap.put(State.audit_approved, new ApprovedAudit());
statusMap.put(State.audit_rejected, new RejectedAudit());
}
public void function(String recordId, Event event) {
State currentState = getStateByRecordIdFromDb(recordId);
function(currentState, event);
}
public void function(State currentState, Event event) {
//1. get state class by currentState
IAudit statusAudit = statusMap.get(currentState);
if (null == statusAudit) {
System.out.println("invalid state = " + currentState.getValue());
return;
}
//2. execute event by event type
statusAudit.triggerEvent(event);
}
}
Author:忆之独秀
Email:leaguenew@qq.com
注明出处:https://blog.csdn.net/lavorange/article/details/128170927