当前位置:   article > 正文

设计模式之状态模式

设计模式之状态模式

经典案例:任务状态流转的过程

  1. private String state = StateEnum.INIT.getStateValue();
  2. public String update(ActionEnum actionEnum){
  3. if (StateEnum.INIT.getStateValue().equals(state)) {
  4. //初始化 -> 进行中
  5. if (ActionEnum.START == actionEnum) {
  6. state = StateEnum.ONGOING.getStateValue();
  7. }
  8. } else if (StateEnum.ONGOING.getStateValue().equals(state)) {
  9. //进行中 -> 已完成 进行中 -> 暂停中 进行中 -> 已过期
  10. if (actionEnum == ActionEnum.ACHIEVE) {
  11. state = StateEnum.FINISHED.getStateValue();
  12. }else if (actionEnum == ActionEnum.STOP){
  13. state = StateEnum.PAUSED.getStateValue();
  14. }else if (actionEnum == ActionEnum.EXPIRE){
  15. state = StateEnum.EXPIRED.getStateValue();
  16. }
  17. }else if (StateEnum.PAUSED.getStateValue().equals(state)) {
  18. //暂停中 -> 进行中 暂停中 -> 已过期
  19. if (actionEnum == ActionEnum.START) {
  20. state = StateEnum.ONGOING.getStateValue();
  21. }else if (actionEnum == ActionEnum.EXPIRE){
  22. state = StateEnum.EXPIRED.getStateValue();
  23. }
  24. }else if (StateEnum.FINISHED.getStateValue().equals(state)) {
  25. }else if (StateEnum.EXPIRED.getStateValue().equals(state)) {
  26. }
  27. return state;
  28. }

        不难看出,状态流转的过程使用了大量的if-else做判断,条件复杂或者状态太多时,条件判断语句会过于臃肿,可读性差,且不具备扩展性,维护难度也大。且增加新的状态时要添加新的if-else语句,这违背了开闭原则,不利于程序的扩展。那么可以使用状态模式解决以上的问题,参考美团:设计模式二三事

        

状态模式:对有状态的对象,把复杂的“判断逻辑”提取到不同的状态对象中,允许状态对象在其内部状态发生改变时改变其行为。状态模式包含以下主要角色:

  • 环境类(Context)角色:也称为上下文,它定义了客户端需要的接口,内部维护一个当前状态,并负责具体状态的切换。

  • 抽象状态(State)角色:定义一个接口,用以封装环境对象中的特定状态所对应的行为,可以有一个或多个行为。

  • 具体状态(Concrete State)角色:实现抽象状态所对应的行为,并且在需要的情况下进行状态切换。

  1. /**
  2. * @author maoyouhua
  3. * @time 2024/3/19
  4. * @jdkversion jdk21
  5. *
  6. * 枚举类
  7. */
  8. @AllArgsConstructor
  9. @Getter
  10. public enum ActionEnum {
  11. START( "开始"),
  12. STOP("暂停"),
  13. ACHIEVE("完成"),
  14. EXPIRE("过期")
  15. ;
  16. private final String actionValue;
  17. }
  18. @AllArgsConstructor
  19. @Getter
  20. public enum StateEnum {
  21. INIT("初始化"),
  22. ONGOING( "进行中"),
  23. PAUSED("暂停中"),
  24. FINISHED("已完成"),
  25. EXPIRED("已过期")
  26. ;
  27. private final String stateValue;
  28. public String getStateValue() {
  29. return stateValue;
  30. }
  31. }
  1. /**
  2. * @author maoyouhua
  3. * @time 2024/3/19
  4. * @jdkversion jdk21
  5. *
  6. * 抽象状态
  7. */
  8. public abstract interface State {
  9. void update(TaskService taskService, ActionEnum actionEnum);
  10. String getCurrentState();
  11. }
  12. public abstract class AbstractState implements State {
  13. }
  1. /**
  2. * @author maoyouhua
  3. * @time 2024/3/19
  4. * @jdkversion jdk21
  5. *
  6. * 具体状态
  7. * 此处有spring循环依赖的问题 需要加配置 spring.main.allow-circular-references=true
  8. */
  9. @Component
  10. public class InitState extends AbstractState {
  11. @Autowired
  12. private OnGoingState onGoingState;
  13. public static final String stateValue = StateEnum.INIT.getStateValue();
  14. @Override
  15. public void update(TaskService taskService, ActionEnum actionEnum) {
  16. //初始化 -> 进行中
  17. if (actionEnum == ActionEnum.START) {
  18. taskService.setTaskState(onGoingState);
  19. }
  20. }
  21. @Override
  22. public String getCurrentState() {
  23. return stateValue;
  24. }
  25. }
  26. @Component
  27. public class OnGoingState extends AbstractState {
  28. @Autowired
  29. private FinishedState finishedState;
  30. @Autowired
  31. private ExpiredState expiredState;
  32. @Autowired
  33. private PausedState pausedState;
  34. public static final String stateValue = StateEnum.ONGOING.getStateValue();
  35. @Override
  36. public void update(TaskService taskService, ActionEnum actionEnum) {
  37. //进行中 -> 已完成 进行中 -> 暂停中 进行中 -> 已过期
  38. if (actionEnum == ActionEnum.ACHIEVE) {
  39. taskService.setTaskState(finishedState);
  40. }else if (actionEnum == ActionEnum.STOP){
  41. taskService.setTaskState(pausedState);
  42. }else if (actionEnum == ActionEnum.EXPIRE){
  43. taskService.setTaskState(expiredState);
  44. }
  45. }
  46. @Override
  47. public String getCurrentState() {
  48. return stateValue;
  49. }
  50. }
  51. @Component
  52. public class PausedState extends AbstractState {
  53. @Autowired
  54. private OnGoingState onGoingState;
  55. @Autowired
  56. private ExpiredState expiredState;
  57. public static final String stateValue = StateEnum.PAUSED.getStateValue();
  58. @Override
  59. public void update(TaskService taskService, ActionEnum actionEnum) {
  60. // 暂停中 -> 进行中 暂停中 -> 已过期
  61. if (actionEnum == ActionEnum.START) {
  62. taskService.setTaskState(onGoingState);
  63. }else if (actionEnum == ActionEnum.EXPIRE){
  64. taskService.setTaskState(expiredState);
  65. }
  66. }
  67. @Override
  68. public String getCurrentState() {
  69. return stateValue;
  70. }
  71. }
  72. @Component
  73. public class FinishedState extends AbstractState {
  74. public static final String stateValue = StateEnum.FINISHED.getStateValue();
  75. @Override
  76. public void update(TaskService taskService, ActionEnum actionEnum) {}
  77. @Override
  78. public String getCurrentState() {
  79. return stateValue;
  80. }
  81. }
  82. @Component
  83. public class ExpiredState extends AbstractState {
  84. public static final String stateValue = StateEnum.EXPIRED.getStateValue();
  85. @Override
  86. public void update(TaskService taskService, ActionEnum actionEnum) {}
  87. @Override
  88. public String getCurrentState() {
  89. return stateValue;
  90. }
  91. }
  1. /**
  2. * @author maoyouhua
  3. * @time 2024/3/19
  4. * @jdkversion jdk21
  5. *
  6. * 多线程不安全
  7. */
  8. public interface TaskService {
  9. void updateState(ActionEnum actionEnum);
  10. State getTaskState();
  11. void setTaskState(State state);
  12. }
  13. @Component
  14. public class TaskServiceImpl implements TaskService {
  15. private State state;
  16. @Autowired
  17. public TaskServiceImpl(InitState initState) {
  18. this.state = initState;
  19. }
  20. @Override
  21. public void updateState(ActionEnum actionEnum){
  22. state.update(this,actionEnum);
  23. }
  24. @Override
  25. public State getTaskState() {
  26. return state;
  27. }
  28. @Override
  29. public void setTaskState(State state) {
  30. this.state = state;
  31. }
  32. }
  1. /**
  2. * @author maoyouhua
  3. * @time 2024/3/19
  4. * @jdkversion jdk21
  5. */
  6. @RestController
  7. @RequestMapping("/task")
  8. public class TaskController {
  9. private String state = StateEnum.INIT.getStateValue();
  10. @Autowired
  11. private TaskService taskService;
  12. @RequestMapping("/test")
  13. public String test(){
  14. return "任务测试";
  15. }
  16. /**
  17. *
  18. * @param actionEnum START、STOP、ACHIEVE、EXPIRE
  19. * @return
  20. */
  21. @RequestMapping("/update")
  22. public String update(ActionEnum actionEnum){
  23. if (StateEnum.INIT.getStateValue().equals(state)) {
  24. //初始化 -> 进行中
  25. if (ActionEnum.START == actionEnum) {
  26. state = StateEnum.ONGOING.getStateValue();
  27. }
  28. } else if (StateEnum.ONGOING.getStateValue().equals(state)) {
  29. //进行中 -> 已完成 进行中 -> 暂停中 进行中 -> 已过期
  30. if (actionEnum == ActionEnum.ACHIEVE) {
  31. state = StateEnum.FINISHED.getStateValue();
  32. }else if (actionEnum == ActionEnum.STOP){
  33. state = StateEnum.PAUSED.getStateValue();
  34. }else if (actionEnum == ActionEnum.EXPIRE){
  35. state = StateEnum.EXPIRED.getStateValue();
  36. }
  37. }else if (StateEnum.PAUSED.getStateValue().equals(state)) {
  38. //暂停中 -> 进行中 暂停中 -> 已过期
  39. if (actionEnum == ActionEnum.START) {
  40. state = StateEnum.ONGOING.getStateValue();
  41. }else if (actionEnum == ActionEnum.EXPIRE){
  42. state = StateEnum.EXPIRED.getStateValue();
  43. }
  44. }else if (StateEnum.FINISHED.getStateValue().equals(state)) {
  45. }else if (StateEnum.EXPIRED.getStateValue().equals(state)) {
  46. }
  47. return state;
  48. }
  49. @RequestMapping("/updateByStateMode")
  50. public String updateByStateMode(ActionEnum actionEnum){
  51. taskService.updateState(actionEnum);
  52. return taskService.getTaskState().getCurrentState();
  53. }
  54. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/274055
推荐阅读
相关标签
  

闽ICP备14008679号