状态模式
状态模式
1.按照一个营销活动的审核状态进行流转
活动编辑,审核,撤销审核,通过,拒绝,关闭,开启,活动中
模拟活动的状态的流转
2.上代码-基础定义
status.java 活动中每个状态的定义
public enum Status {
// 1创建编辑、2待审核、3审核通过(任务扫描成活动中)、4审核拒绝(可以撤审到编辑状态)、 5活动中、6活动关闭、7活动开启(任务扫描成活动中)
Editing, Check, Pass, Refuse, Doing, Close, Open;
}
ActivityService.java 活动服务,主要用于执行业务逻辑及维护活动id和当前状态的关系
public class ActivityService {
/*
活动id->活动状态的关系
*/
private static Map<String,Status> statusMap = Maps.newConcurrentMap();
public static void init(String activityId,Status status){
statusMap.put(activityId,status);
}
public static Status queryActivityStatus(String activityId){
return statusMap.get(activityId);
}
public static synchronized void execStatus(String activityId,Status beforeStatus,Status afterStatus){
if (beforeStatus.equals(statusMap.get(activityId))){
return;
}
statusMap.put(activityId,afterStatus);
}
}
3.上代码-状态的定义
State是一个抽象类,定义了各种操作接口(提审,审核,拒审)
State.java
public abstract class State {
/**
* 活动审核
* @param activityId
* @param currentStatus
* @return
*/
public abstract Result arraignment(String activityId,Status currentStatus);
/**
* 审核通过
* @param activityId
* @param currentStatus
* @return
*/
public abstract Result checkPass(String activityId,Status currentStatus);
/**
* 审核拒绝
* @param activityId
* @param currentStatus
* @return
*/
public abstract Result checkRefuse(String activityId,Status currentStatus);
/**
* 撤审撤消
* @param activityId
* @param currentStatus
* @return
*/
public abstract Result checkRevoke(String activityId,Status currentStatus);
/**
* 活动关闭
* @param activityId
* @param currentStatus
* @return
*/
public abstract Result close(String activityId,Status currentStatus);
/**
* 活动开启
* @param activityId
* @param currentStatus
* @return
*/
public abstract Result open(String activityId,Status currentStatus);
/**
* 活动执行
* @param activityId
* @param currentStatus
* @return
*/
public abstract Result doing(String activityId,Status currentStatus);
}
4.上代码-修改状态的实现类
EditingState.java 对提审状态的修改逻辑
public class EditingState extends State{
/**
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result arraignment(String activityId, Status currentStatus) {
ActivityService.execStatus(activityId,currentStatus,Status.Editing);
return Result.of("活动提审成功").setCode("0000");
}
/**
* 审核通过
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result checkPass(String activityId, Status currentStatus) {
return Result.of("编辑中不可审核通过").setCode("0001");
}
/**
* 审核拒绝
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result checkRefuse(String activityId, Status currentStatus) {
return Result.of("编辑中不可审核拒绝").setCode("0001");
}
/**
* 撤审撤消
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result checkRevoke(String activityId, Status currentStatus) {
return Result.of("编辑中不可撤销审核").setCode("0001");
}
/**
* 活动关闭
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result close(String activityId, Status currentStatus) {
return Result.of("活动关闭成功").setCode("0001");
}
/**
* 活动开启
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result open(String activityId, Status currentStatus) {
return Result.of("非关闭活动不可开启").setCode("0001");
}
/**
* 活动执行
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result doing(String activityId, Status currentStatus) {
return Result.of("编辑中活动不可执行活动中变更").setCode("0001");
}
}
CheckState.java 审核状态的变更实现类
public class CheckState extends State{
/**
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result arraignment(String activityId, Status currentStatus) {
return Result.of("待审核状态不可重复提审").setCode("0001");
}
/**
* 审核通过
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result checkPass(String activityId, Status currentStatus) {
ActivityService.execStatus(activityId,currentStatus,Status.Pass);
return Result.of("活动审核通过完成").setCode("0000");
}
/**
* 审核拒绝
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result checkRefuse(String activityId, Status currentStatus) {
ActivityService.execStatus(activityId,currentStatus,Status.Refuse);
return Result.of("活动审核拒绝完成").setCode("0000");
}
/**
* 撤审撤消
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result checkRevoke(String activityId, Status currentStatus) {
ActivityService.execStatus(activityId,currentStatus,Status.Editing);
return Result.of("活动审核撤销回到编辑中").setCode("0000");
}
/**
* 活动关闭
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result close(String activityId, Status currentStatus) {
ActivityService.execStatus(activityId,currentStatus,Status.Close);
return Result.of("活动审核关闭完成").setCode("0000");
}
/**
* 活动开启
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result open(String activityId, Status currentStatus) {
return Result.of("非关闭活动不可开启").setCode("0001");
}
/**
* 活动执行
*
* @param activityId
* @param currentStatus
* @return
*/
@Override
public Result doing(String activityId, Status currentStatus) {
return Result.of("待审核活动不不可执⾏行行活动中变更更").setCode("0001");
}
}
5.上代码-状态处理handler
StateHandler.java // 每一个状态对象了一个状态的修改实现类
public class StateHandler {
private Map<Status,State> stateMap = Maps.newConcurrentMap();
public StateHandler() {
stateMap.put(Status.Editing,new EditingState());
stateMap.put(Status.Check,new CheckState());
stateMap.put(Status.Close,new CheckState());
}
public Result arraignment(String activityId,Status currentStatus){
return stateMap.get(currentStatus).arraignment(activityId,currentStatus);
}
public Result checkPass(String activityId,Status currentStatus){
return stateMap.get(currentStatus).checkPass(activityId,currentStatus);
}
public Result checkRefuse(String activityId,Status currentStatus){
return stateMap.get(currentStatus).checkRefuse(activityId,currentStatus);
}
public Result checkRevoke(String activityId,Status currentStatus){
return stateMap.get(currentStatus).checkRevoke(activityId,currentStatus);
}
public Result close(String activityId,Status currentStatus){
return stateMap.get(currentStatus).close(activityId,currentStatus);
}
public Result open(String activityId,Status currentStatus){
return stateMap.get(currentStatus).open(activityId,currentStatus);
}
public Result doing(String activityId,Status currentStatus){
return stateMap.get(currentStatus).doing(activityId,currentStatus);
}
}
6.上代码-测试类
StateTest.java
@Slf4j
public class StateTest {
@Test
public void test(){
// 活动id
String activityId = "100001";
// 初始化活动的初试状态为editing
ActivityService.init(activityId,Status.Editing);
StateHandler stateHandler = Singleton.get(StateHandler.class);
// 编辑
Result result = stateHandler.arraignment(activityId, Status.Editing);
log.info("测试结果:{}",JsonUtils.toJSONString(result));
log.info("活动信息:{}",
ActivityService.queryActivityStatus(activityId));
// 审核通过
Result result1 = stateHandler.checkPass(activityId, Status.Check);
log.info("测试结果:{}",JsonUtils.toJSONString(result1));
log.info("活动信息:{}",
ActivityService.queryActivityStatus(activityId));
// 活动结束
Result result2 = stateHandler.close(activityId, Status.Close);
log.info("测试结果:{}",JsonUtils.toJSONString(result2));
log.info("活动信息:{}",
ActivityService.queryActivityStatus(activityId));
}
}
测试结果
15:14:42.514 [main] INFO s.StateTest - 测试结果:{"data":"活动提审成功","code":"0000"}
15:14:42.518 [main] INFO s.StateTest - 活动信息:Editing
15:14:42.519 [main] INFO s.StateTest - 测试结果:{"data":"活动审核通过完成","code":"0000"}
15:14:42.519 [main] INFO s.StateTest - 活动信息:Pass
15:14:42.519 [main] INFO s.StateTest - 测试结果:{"data":"活动审核关闭完成","code":"0000"}
15:14:42.519 [main] INFO s.StateTest - 活动信息:Close
原创:做时间的朋友