状态模式是什么?

状态模式(State Design)是一种行为型的设计模式。根据 GoF 对状态录模式的定义:

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

这句话的意思是,允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。简单地说就是当一个对象的状态发生变化时,它的行为也会跟着变化。电视机的开关就是个典型的例子,当电视机是关着的时候,我们按下开关按钮,电视机就会被打开;这时再按下开关按钮,电视机就会被关掉。如果我们要用代码实现这个开关按钮的功能,那么我们一般可能会这么写:

  1. public class SwitchButton {
  2. private State currentState;
  3. public SwitchButton(State currentState) {
  4. this.currentState = currentState;
  5. }
  6. public void push() {
  7. if (State.ON.equals(currentState)) {
  8. currentState = State.OFF;
  9. System.out.println("The television is turned off.");
  10. } else {
  11. currentState = State.ON;
  12. System.out.println("The television is turned on.");
  13. }
  14. }
  15. }

当对象的状态变化逻辑比较简单时,这样的实现是可以的。但在实际的系统开发中,对象的状态变化往往是比较复杂的,这样会产生繁杂的状态判断逻辑(大量的 if-else),不利于系统后期的维护和扩展。在状态模式中,将状态的判断、变化逻辑转移到表示不同状态的一系列类中,这样每一个类代表着一种状态,只专注于这种状态的逻辑,简化了状态判断的处理逻辑,同时也增强的系统的可读性,更利于系统的维护和扩展。

案例

让我们通过一个案例来帮助我们进一步理解状态模式。假设现在有一个在线请假系统,员工可在系统申请休假。员工申请休假时要先填写申请单,内容填写后申请单是处于草稿状态;员工确认填写无误后,就可以提交申请,申请单的状态变为待审批;如果员工取消休假申请,那么申请单则变为关闭状态;经理收到申请单后,可以批准,那么申请单的状态变为完成,整个休假申请的流程就结束了;如果经理不同意员工的休假申请,驳回申请单,那么申请单也会被关闭。下图描述了这个系统中,申请单的状态变化流程。
image.png
首先,我们要定义一个抽象的申请单状态来描述这些状态有哪些变化的行为。

  1. public interface LeaveState {
  2. boolean nextStep(Leave leave);
  3. boolean close(Leave leave);
  4. }

接下来是定义各个具体的申请单状态,草稿状态:

  1. public class DraftState implements LeaveState {
  2. @Override
  3. public boolean nextStep(Leave leave) {
  4. leave.setCurrentState(new PendingState());
  5. System.out.println("This leave is pending now.");
  6. return true;
  7. }
  8. @Override
  9. public boolean close(Leave leave) {
  10. leave.setCurrentState(new ClosedState());
  11. System.out.println("This leave is closed now.");
  12. return true;
  13. }
  14. }

待审批状态:

  1. public class PendingState implements LeaveState {
  2. @Override
  3. public boolean nextStep(Leave leave) {
  4. leave.setCurrentState(new CompletedState());
  5. System.out.println("This leave is completed now.");
  6. return true;
  7. }
  8. @Override
  9. public boolean close(Leave leave) {
  10. leave.setCurrentState(new ClosedState());
  11. System.out.println("This leave is closed now.");
  12. return true;
  13. }
  14. }

完成状态:

  1. public class CompletedState implements LeaveState {
  2. @Override
  3. public boolean nextStep(Leave leave) {
  4. System.out.println("This leave has been completed, you can't operate it any more.");
  5. return false;
  6. }
  7. @Override
  8. public boolean close(Leave leave) {
  9. System.out.println("This leave has been completed, you can't operate it any more.");
  10. return false;
  11. }
  12. }

关闭状态:

  1. public class ClosedState implements LeaveState {
  2. @Override
  3. public boolean nextStep(Leave leave) {
  4. System.out.println("This leave has been closed, you can't operate it any more.");
  5. return false;
  6. }
  7. @Override
  8. public boolean close(Leave leave) {
  9. System.out.println("This leave has been closed, you can't operate it any more.");
  10. return false;
  11. }
  12. }

申请单含有发起员工、申请经理、休假时间、休假原因等信息,它的状态变化逻辑则是交由它当前具体的状态控制。

  1. public class Leave {
  2. private Staff initiator;
  3. private Manager approver;
  4. private Date startDate;
  5. private Date endDate;
  6. private String reason;
  7. private LeaveState currentState;
  8. // constructor, getters and setters
  9. public boolean nextStep() {
  10. return currentState.nextStep(this);
  11. }
  12. public boolean close() {
  13. return currentState.close(this);
  14. }
  15. }

Staff 类描述了员工的行为,他可以发起休假申请、提交申请、撤消申请。

  1. public class Staff {
  2. public Leave askForLeave(Date startDate, Date endDate, String reason) {
  3. System.out.println("The staff is asking for leave.");
  4. return new Leave(this, startDate, endDate, reason);
  5. }
  6. public void submitLeave(Leave leave) {
  7. System.out.println("The staff is submitting the leave application.");
  8. leave.nextStep();
  9. }
  10. public void withdrawLeave(Leave leave) {
  11. System.out.println("The staff is withdrawing the leave application.");
  12. leave.close();
  13. }
  14. }

Manager 类描述了经理的行为,他可以批准或驳回员工的休假申请。

  1. public class Manager {
  2. public void approveLeave(Leave leave) {
  3. System.out.println("The manager is approving the leave application.");
  4. if (leave.nextStep()) {
  5. leave.setApprover(this);
  6. }
  7. }
  8. public void disapproveLeave(Leave leave) {
  9. System.out.println("The manager is disapproving the leave application.");
  10. if (leave.close()) {
  11. leave.setApprover(this);
  12. }
  13. }
  14. }

最后,我们通过一个用例来模拟一下员工申请休假、经理批准的情景:

  1. public class StateMain {
  2. public static void main(String[] args) {
  3. Staff staff = new Staff();
  4. Date today = new Date();
  5. Leave leave = staff.askForLeave(today, today, "personal affairs");
  6. staff.submitLeave(leave);
  7. Manager manager = new Manager();
  8. manager.approveLeave(leave);
  9. }
  10. }

案例源码

可在 GitHub 上查看上述案例的完整代码。

参考资料

本文参考的资料如下: