登录
与 不登录
就是我们通过改变 状态,而让整个行为发生了变化。这在设计模式里也是一个关键的点
)。pom.xml
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.5version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>jcl-over-slf4jartifactId>
<version>1.7.5version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.0.9version>
<exclusions>
<exclusion>
<artifactId>slf4j-apiartifactId>
<groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
dependencies>
design-20.0-0
|——src
|——main
|--java
|--com.lino.design
|-ActicityInfo.java
|-ActivityService.java
|-Result.java
|-Status.java
Status.java
package com.lino.design;
/**
* @description: 状态枚举
*/
public enum Status {
/**
* 1.创建编辑,2.待审核,3.审核通过(任务扫描成活动中),
* 4.审核拒绝(可以撤审到编辑状态),5.活动中,
* 6.活动关闭,7.活动开启(任务扫描成活动中)
*/
Editing, Check, Pass, Refuse, Doing, Close, Open
}
ActicityInfo.java
package com.lino.design;
import java.util.Date;
import java.util.Map;
/**
* @description: 活动类
*/
public class ActicityInfo {
/**
* 活动ID
*/
private String acticityId;
/**
* 活动名称
*/
private String acticityName;
/**
* 活动状态
*/
private Enum<Status> status;
/**
* 开始时间
*/
private Date beginTime;
/**
* 结束时间
*/
private Date endTime;
public String getActicityId() {
return acticityId;
}
public void setActicityId(String acticityId) {
this.acticityId = acticityId;
}
public String getActicityName() {
return acticityName;
}
public void setActicityName(String acticityName) {
this.acticityName = acticityName;
}
public Enum<Status> getStatus() {
return status;
}
public void setStatus(Enum<Status> status) {
this.status = status;
}
public Date getBeginTime() {
return beginTime;
}
public void setBeginTime(Date beginTime) {
this.beginTime = beginTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
}
ID
、活动名称、活动状态、开始时间、结束时间ActivityService.java
package com.lino.design;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @description: 活动服务类
*/
public class ActivityService {
private static Map<String, Enum<Status>> statusMap = new ConcurrentHashMap<>();
/**
* 初始化活动
*
* @param activityId 活动ID
* @param status 活动状态
*/
public static void init(String activityId, Enum<Status> status) {
// 模拟查询活动信息
ActicityInfo acticityInfo = new ActicityInfo();
acticityInfo.setActicityId(activityId);
acticityInfo.setActicityName("早起学习打卡领奖活动");
acticityInfo.setStatus(status);
acticityInfo.setBeginTime(new Date());
acticityInfo.setEndTime(new Date());
statusMap.put(activityId, status);
}
/**
* 查询活动信息
*
* @param activityId 活动ID
* @return 活动信息
*/
public static ActicityInfo queryActivityInfo(String activityId) {
// 模拟查询活动信息
ActicityInfo acticityInfo = new ActicityInfo();
acticityInfo.setActicityId(activityId);
acticityInfo.setActicityName("早起学习打卡领奖活动");
acticityInfo.setStatus(statusMap.get(activityId));
acticityInfo.setBeginTime(new Date());
acticityInfo.setEndTime(new Date());
return acticityInfo;
}
/**
* 查询活动状态
*
* @param activityId 活动ID
* @return 活动状态
*/
public static Enum<Status> queryActivityStatus(String activityId) {
return statusMap.get(activityId);
}
/**
* 执行状态变更
*
* @param activityId 活动ID
* @param beforeStatus 变更前状态
* @param afterStatus 变更后状态
*/
public static synchronized void execStatus(String activityId, Enum<Status> beforeStatus, Enum<Status> afterStatus) {
if (!beforeStatus.equals(statusMap.get(activityId))) {
return;
}
statusMap.put(activityId, afterStatus);
}
}
queryActivityInfo
:查询活动信息。queryActivityStatus
:查询活动状态。execStatus
:执行状态变更。Map
的结构来记录活动ID
和状态变化信息,另外还有 init
方法来初始化活动数据。
数据库
或者 Redis
中获取。Result.java
package com.lino.design;
/**
* @description: 结果类
*/
public class Result {
/**
* 编码
*/
private String code;
/**
* 描述
*/
private String info;
public Result(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
对于这样各种状态的变更,最让我们直接想到的就是使用
if
和else
进行判断处理。
每一个状态可以流转到下一个什么状态,都可以使用嵌套的if
实现。
<dependencies>
<dependency>
<groupId>com.linogroupId>
<artifactId>design-20.0-0artifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
design-20.0-1
|——src
|——main
|--java
|--com.lino.design
|-ActivityExecStatusController.java
|--test
|--com.lino.design.test
|-ApiTest.java
ActivityExecStatusController.java
package com.lino.design;
/**
* @description: 活动状态变更控制层
*/
public class ActivityExecStatusController {
/**
* 活动状态变更
* 1. 编辑中 -> 提审、关闭
* 2. 审核通过 -> 拒绝、关闭、活动中
* 3. 审核拒绝 -> 撤审、关闭
* 4. 活动中 -> 关闭
* 5. 活动关闭 -> 开启
* 6. 活动开启 -> 关闭
*
* @param activityId 活动ID
* @param beforeStatus 变更前状态
* @param afterStatus 变更后状态
* @return 返回结果
*/
public Result execStatus(String activityId, Enum<Status> beforeStatus, Enum<Status> afterStatus) {
// 1. 编辑中 -> 提审、关闭
if (Status.Editing.equals(beforeStatus)) {
if (Status.Check.equals(afterStatus) || Status.Close.equals(afterStatus)) {
ActivityService.execStatus(activityId, beforeStatus, afterStatus);
return new Result("0000", "变更状态成功");
} else {
return new Result("0001", "变更状态拒绝");
}
}
// 2. 审核通过 -> 拒绝、关闭、活动中
if (Status.Pass.equals(beforeStatus)) {
if (Status.Refuse.equals(afterStatus) || Status.Doing.equals(afterStatus) || Status.Close.equals(afterStatus)) {
ActivityService.execStatus(activityId, beforeStatus, afterStatus);
return new Result("0000", "变更状态成功");
} else {
return new Result("0001", "变更状态拒绝");
}
}
// 3. 审核拒绝 -> 撤审、关闭
if (Status.Refuse.equals(beforeStatus)) {
if (Status.Editing.equals(afterStatus) || Status.Close.equals(afterStatus)) {
ActivityService.execStatus(activityId, beforeStatus, afterStatus);
return new Result("0000", "变更状态成功");
} else {
return new Result("0001", "变更状态拒绝");
}
}
// 4. 活动中 -> 关闭
if (Status.Doing.equals(beforeStatus)) {
if (Status.Close.equals(afterStatus)) {
ActivityService.execStatus(activityId, beforeStatus, afterStatus);
return new Result("0000", "变更状态成功");
} else {
return new Result("0001", "变更状态拒绝");
}
}
// 5. 活动关闭 -> 开启
if (Status.Close.equals(beforeStatus)) {
if (Status.Open.equals(afterStatus)) {
ActivityService.execStatus(activityId, beforeStatus, afterStatus);
return new Result("0000", "变更状态成功");
} else {
return new Result("0001", "变更状态拒绝");
}
}
// 6. 活动开启 -> 关闭
if (Status.Open.equals(beforeStatus)) {
if (Status.Close.equals(afterStatus)) {
ActivityService.execStatus(activityId, beforeStatus, afterStatus);
return new Result("0000", "变更状态成功");
} else {
return new Result("0001", "变更状态拒绝");
}
}
return new Result("0001", "非可处理的活动状态变更");
}
}
if-else
。但基本不可能不迭代
)。ApiTest.java
package com.lino.design.test;
import com.alibaba.fastjson.JSON;
import com.lino.design.ActivityExecStatusController;
import com.lino.design.ActivityService;
import com.lino.design.Result;
import com.lino.design.Status;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @description: 单元测试
*/
public class ApiTest {
private Logger logger = LoggerFactory.getLogger(ApiTest.class);
@Test
public void test() {
// 初始化数据
String activityId = "100001";
ActivityService.init(activityId, Status.Editing);
ActivityExecStatusController activityExecStatusController = new ActivityExecStatusController();
Result resultRefuse = activityExecStatusController.execStatus(activityId, Status.Editing, Status.Refuse);
logger.info("测试结果(编辑中To审核拒绝):{}", JSON.toJSONString(resultRefuse));
Result resultCheck = activityExecStatusController.execStatus(activityId, Status.Editing, Status.Check);
logger.info("测试结果(编辑中To提交审核):{}", JSON.toJSONString(resultCheck));
}
}
编辑中
到 审核拒绝
,另外一个是从 编辑中
到 提交审核
。编辑中
的活动是不能直接到 审核拒绝
,这中间还需要 提审
。测试结果
17:03:34.528 [main] INFO com.lino.design.test.ApiTest - 测试结果(编辑中To审核拒绝):{"code":"0001","info":"变更状态拒绝"}
17:03:34.534 [main] INFO com.lino.design.test.ApiTest - 测试结果(编辑中To提交审核):{"code":"0000","info":"变更状态成功"}
重构的重点往往是处理掉
if-else
,而想处理掉if-else
基本离不开接口与抽象类,另外还需要重新改造代码结构。
<dependencies>
<dependency>
<groupId>com.linogroupId>
<artifactId>design-20.0-0artifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
design-19.0-2
|——src
|——main
|--java
|--com.lino.design
|--impl
| |--CheckState.java
| |--CloseState.java
| |--DoingState.java
| |--EditingState.java
| |--OpenState.java
| |--PassState.java
| |--RefuseState.java
|-State.java
|-StateHandler.java
|--test
|--com.lino.design.test
|-ApiTest.java
State
是一个抽象类,定义了各种操作接口(提审、审核、拒审等
)。if
语言进行判断了。StateHandler
对状态流程的统一处理,里面提供 Map
结构的各项服务接口调用,也就避免了使用 if
判断各项状态转变的流程。State.java
package com.lino.design;
/**
* @description: 活动状态抽象类
*/
public abstract class State {
/**
* 活动提审
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public abstract Result arraignment(String activityId, Enum<Status> currentStatus);
/**
* 审核通过
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public abstract Result checkPass(String activityId, Enum<Status> currentStatus);
/**
* 审核拒绝
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public abstract Result checkRefuse(String activityId, Enum<Status> currentStatus);
/**
* 撤审撤销
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public abstract Result checkRevoke(String activityId, Enum<Status> currentStatus);
/**
* 活动关闭
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public abstract Result close(String activityId, Enum<Status> currentStatus);
/**
* 活动开启
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public abstract Result open(String activityId, Enum<Status> currentStatus);
/**
* 活动执行
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public abstract Result doing(String activityId, Enum<Status> currentStatus);
}
活动提审
、审核通过
、审核拒绝
、撤审撤销
、活动关闭
、活动开启
、**活动执行
。activityId
(活动ID)、currentStatus
(当前状态),只有他们的具体实现是不同的。CheckState.java
package com.lino.design.impl;
import com.lino.design.ActivityService;
import com.lino.design.Result;
import com.lino.design.State;
import com.lino.design.Status;
/**
* @description: 待审核
*/
public class CheckState extends State {
@Override
public Result arraignment(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "待审核状态不可重复提审");
}
@Override
public Result checkPass(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Pass);
return new Result("0000", "活动审核通过完成");
}
@Override
public Result checkRefuse(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Refuse);
return new Result("0000", "活动审核拒绝完成");
}
@Override
public Result checkRevoke(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Editing);
return new Result("0000", "活动审核撤销回到编辑中");
}
@Override
public Result close(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Close);
return new Result("0000", "活动审核关闭完成");
}
@Override
public Result open(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "非关闭活动不可开启");
}
@Override
public Result doing(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "待审核活动不可执行活动中变更");
}
}
CloseState.java
package com.lino.design.impl;
import com.lino.design.ActivityService;
import com.lino.design.Result;
import com.lino.design.State;
import com.lino.design.Status;
/**
* @description: 活动关闭
*/
public class CloseState extends State {
@Override
public Result arraignment(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动关闭不可提审");
}
@Override
public Result checkPass(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动关闭不可审核通过");
}
@Override
public Result checkRefuse(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动关闭不可审核拒绝");
}
@Override
public Result checkRevoke(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动关闭不可撤销审核");
}
@Override
public Result close(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动关闭不可重复关闭");
}
@Override
public Result open(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Open);
return new Result("0000", "活动开启完成");
}
@Override
public Result doing(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动关闭不可变更活动中");
}
}
DoingState.java
package com.lino.design.impl;
import com.lino.design.ActivityService;
import com.lino.design.Result;
import com.lino.design.State;
import com.lino.design.Status;
/**
* @description: 活动中
*/
public class DoingState extends State {
@Override
public Result arraignment(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动中不可提审");
}
@Override
public Result checkPass(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动中不可审核通过");
}
@Override
public Result checkRefuse(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动中不可审核拒绝");
}
@Override
public Result checkRevoke(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动中不可撤销审核");
}
@Override
public Result close(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Close);
return new Result("0000", "活动关闭完成");
}
@Override
public Result open(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动中不可开启");
}
@Override
public Result doing(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动中不可重复执行");
}
}
EditingState.java
package com.lino.design.impl;
import com.lino.design.ActivityService;
import com.lino.design.Result;
import com.lino.design.State;
import com.lino.design.Status;
/**
* @description: 编辑中
*/
public class EditingState extends State {
@Override
public Result arraignment(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Check);
return new Result("0000", "活动提审成功");
}
@Override
public Result checkPass(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "编辑中不可审核通过");
}
@Override
public Result checkRefuse(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "编辑中不可审核拒绝");
}
@Override
public Result checkRevoke(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "编辑中不可撤销审核");
}
@Override
public Result close(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Close);
return new Result("0000", "活动关闭完成");
}
@Override
public Result open(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "非关闭活动不可开启");
}
@Override
public Result doing(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "编辑活动中不可执行活动中变更");
}
}
OpenState.java
package com.lino.design.impl;
import com.lino.design.ActivityService;
import com.lino.design.Result;
import com.lino.design.State;
import com.lino.design.Status;
/**
* @description: 活动开启
*/
public class OpenState extends State {
@Override
public Result arraignment(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动开启不可提审");
}
@Override
public Result checkPass(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动开启不可审核通过");
}
@Override
public Result checkRefuse(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动开启不可审核拒绝");
}
@Override
public Result checkRevoke(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动开启不可撤销审核");
}
@Override
public Result close(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Close);
return new Result("0000", "活动关闭完成");
}
@Override
public Result open(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "活动不可重复开启");
}
@Override
public Result doing(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Doing);
return new Result("0000", "活动变更活动中完成");
}
}
PassState.java
package com.lino.design.impl;
import com.lino.design.ActivityService;
import com.lino.design.Result;
import com.lino.design.State;
import com.lino.design.Status;
/**
* @description: 审核通过
*/
public class PassState extends State {
@Override
public Result arraignment(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "已审核状态不可重复提审");
}
@Override
public Result checkPass(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "已审核状态不可重复审核");
}
@Override
public Result checkRefuse(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Refuse);
return new Result("0000", "活动审核拒绝完成");
}
@Override
public Result checkRevoke(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "审核通过不可撤销(可先拒绝审核)");
}
@Override
public Result close(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Close);
return new Result("0000", "活动关闭完成");
}
@Override
public Result open(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "非关闭活动不可开启");
}
@Override
public Result doing(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Doing);
return new Result("0000", "活动变更活动中完成");
}
}
RefuseState.java
package com.lino.design.impl;
import com.lino.design.ActivityService;
import com.lino.design.Result;
import com.lino.design.State;
import com.lino.design.Status;
/**
* @description: 审核拒绝
*/
public class RefuseState extends State {
@Override
public Result arraignment(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "已审核状态不可重复提审");
}
@Override
public Result checkPass(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "已审核状态不可重复审核");
}
@Override
public Result checkRefuse(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Refuse);
return new Result("0000", "活动审核拒绝不可重复审核");
}
@Override
public Result checkRevoke(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Editing);
return new Result("0000", "撤销审核完成");
}
@Override
public Result close(String activityId, Enum<Status> currentStatus) {
ActivityService.execStatus(activityId, currentStatus, Status.Close);
return new Result("0000", "活动审核关闭完成");
}
@Override
public Result open(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "非关闭活动不可开启");
}
@Override
public Result doing(String activityId, Enum<Status> currentStatus) {
return new Result("0001", "审核拒绝不可执行活动为进行中");
}
}
待审核
、活动关闭
、活动中
、编辑中
、活动开启
、审核通过
、审核拒绝
。StateHandler.java
package com.lino.design;
import com.lino.design.impl.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @description: 活动执行者
*/
public class StateHandler {
private Map<Enum<Status>, State> stateMap = new ConcurrentHashMap<>();
public StateHandler() {
// 待审核
stateMap.put(Status.Check, new CheckState());
// 已关闭
stateMap.put(Status.Close, new CloseState());
// 活动中
stateMap.put(Status.Doing, new DoingState());
// 编辑中
stateMap.put(Status.Editing, new EditingState());
// 已开启
stateMap.put(Status.Open, new OpenState());
// 审核通过
stateMap.put(Status.Pass, new PassState());
// 审核拒绝
stateMap.put(Status.Refuse, new RefuseState());
}
/**
* 活动提审
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public Result arraignment(String activityId, Enum<Status> currentStatus) {
return stateMap.get(currentStatus).arraignment(activityId, currentStatus);
}
/**
* 审核通过
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public Result checkPass(String activityId, Enum<Status> currentStatus) {
return stateMap.get(currentStatus).checkPass(activityId, currentStatus);
}
/**
* 审核拒绝
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public Result checkRefuse(String activityId, Enum<Status> currentStatus) {
return stateMap.get(currentStatus).checkRefuse(activityId, currentStatus);
}
/**
* 撤审撤销
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public Result checkRevoke(String activityId, Enum<Status> currentStatus) {
return stateMap.get(currentStatus).checkRevoke(activityId, currentStatus);
}
/**
* 活动关闭
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public Result close(String activityId, Enum<Status> currentStatus) {
return stateMap.get(currentStatus).close(activityId, currentStatus);
}
/**
* 活动开启
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public Result open(String activityId, Enum<Status> currentStatus) {
return stateMap.get(currentStatus).open(activityId, currentStatus);
}
/**
* 活动执行
*
* @param activityId 活动ID
* @param currentStatus 当前状态
* @return 执行结果
*/
public Result doing(String activityId, Enum<Status> currentStatus) {
return stateMap.get(currentStatus).doing(activityId, currentStatus);
}
}
Map
数据结构中。ApiTest.java
@Test
public void test_Editing2Arraignment() {
String activityId = "100001";
ActivityService.init(activityId, Status.Editing);
StateHandler stateHandler = new StateHandler();
Result result = stateHandler.arraignment(activityId, Status.Editing);
logger.info("测试结果(编辑中To提审活动):{}", JSON.toJSONString(result));
logger.info("活动信息:{} 状态:{}", JSON.toJSONString(ActivityService.queryActivityInfo(activityId)), JSON.toJSONString(ActivityService.queryActivityStatus(activityId)));
}
测试结果
20:48:10.752 [main] INFO com.lino.design.test.ApiTest - 测试结果(编辑中To提审活动):{"code":"0000","info":"活动提审成功"}
20:48:10.788 [main] INFO com.lino.design.test.ApiTest - 活动信息:{"activityId":"100001","activityName":"早起学习打卡领奖活动","beginTime":1675860490764,"endTime":1675860490764,"status":"Check"} 状态:"Check"
ApiTest.java
@Test
public void test_Editing2Open() {
String activityId = "100001";
ActivityService.init(activityId, Status.Editing);
StateHandler stateHandler = new StateHandler();
Result result = stateHandler.open(activityId, Status.Editing);
logger.info("测试结果(编辑中To开启活动):{}", JSON.toJSONString(result));
logger.info("活动信息:{} 状态:{}", JSON.toJSONString(ActivityService.queryActivityInfo(activityId)), JSON.toJSONString(ActivityService.queryActivityStatus(activityId)));
}
测试结果
20:48:57.997 [main] INFO com.lino.design.test.ApiTest - 测试结果(编辑中To开启活动):{"code":"0001","info":"非关闭活动不可开启"}
20:48:58.041 [main] INFO com.lino.design.test.ApiTest - 活动信息:{"activityId":"100001","activityName":"早起学习打卡领奖活动","beginTime":1675860538008,"endTime":1675860538008,"status":"Editing"} 状态:"Editing"
非关闭活动不可开启
。ApiTest.java
@Test
public void test_Editing2Doing() {
String activityId = "100001";
ActivityService.init(activityId, Status.Refuse);
StateHandler stateHandler = new StateHandler();
Result result = stateHandler.doing(activityId, Status.Refuse);
logger.info("测试结果(拒绝To活动中):{}", JSON.toJSONString(result));
logger.info("活动信息:{} 状态:{}", JSON.toJSONString(ActivityService.queryActivityInfo(activityId)), JSON.toJSONString(ActivityService.queryActivityStatus(activityId)));
}
测试结果
20:49:51.974 [main] INFO com.lino.design.test.ApiTest - 测试结果(拒绝To活动中):{"code":"0001","info":"审核拒绝不可执行活动为进行中"}
20:49:52.015 [main] INFO com.lino.design.test.ApiTest - 活动信息:{"activityId":"100001","activityName":"早起学习打卡领奖活动","beginTime":1675860591979,"endTime":1675860591979,"status":"Refuse"} 状态:"Refuse"
审批拒绝不可执行活动中
为进行中,满足预期。ApiTest.java
@Test
public void test_Editing2Revoke() {
String activityId = "100001";
ActivityService.init(activityId, Status.Refuse);
StateHandler stateHandler = new StateHandler();
Result result = stateHandler.checkRevoke(activityId, Status.Refuse);
logger.info("测试结果(拒绝To撤审):{}", JSON.toJSONString(result));
logger.info("活动信息:{} 状态:{}", JSON.toJSONString(ActivityService.queryActivityInfo(activityId)), JSON.toJSONString(ActivityService.queryActivityStatus(activityId)));
}
测试结果
20:50:39.921 [main] INFO com.lino.design.test.ApiTest - 测试结果(拒绝To撤审):{"code":"0000","info":"撤销审核完成"}
20:50:39.960 [main] INFO com.lino.design.test.ApiTest - 活动信息:{"activityId":"100001","activityName":"早起学习打卡领奖活动","beginTime":1675860639937,"endTime":1675860639937,"status":"Editing"} 状态:"Editing"
综上,以上四个测试类分别模拟了不同状态之间到有效流转和拒绝流转,不同的状态服务处理不同的服务内容。
if-else
,代码的结构也更加清晰,易于扩展。