⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 turnstyle.java

📁 著名的uncle Bob的Agile software development的代码
💻 JAVA
字号:
import java.util.Vector;

public class Turnstyle{  // States  public static final int LOCKED = 0;  public static final int UNLOCKED = 1;  // Events  public static final int COIN = 0;  public static final int PASS = 1;
  /*private*/ int state = LOCKED;  private TurnstyleController turnstyleController;  private Vector transitions = new Vector();

  private interface Action
  {
    void execute();
  }

  private class Transition
  {
    public Transition(int currentState, int event, int newState, Action action)
    {
      this.currentState = currentState;
      this.event = event;
      this.newState = newState;
      this.action = action;
    }

    int currentState;
    int event;
    int newState;
    Action action;
  }

  public Turnstyle(TurnstyleController action)
  {
    turnstyleController = action;
    addTransition(LOCKED,   COIN, UNLOCKED, unlock()  );
    addTransition(LOCKED,   PASS, LOCKED,   alarm()   );
    addTransition(UNLOCKED, COIN, UNLOCKED, thankyou());
    addTransition(UNLOCKED, PASS, LOCKED,   lock()    );
  }

  private void addTransition(int currentState, int event, int newState, Action action)
  {
    transitions.add(new Transition(currentState, event, newState, action));
  }

  private Action lock()
  {
    return new Action(){public void execute(){doLock();}};
  }

  private Action thankyou()
  {
    return new Action(){public void execute(){doThankyou();}};
  }

  private Action alarm()
  {
    return new Action(){public void execute(){doAlarm();}};
  }

  private Action unlock()
  {
    return new Action(){public void execute(){doUnlock();}};
  }
  private void doUnlock()  {    turnstyleController.unlock();  }  private void doLock()  {    turnstyleController.lock();  }  private void doAlarm()  {    turnstyleController.alarm();  }  private void doThankyou()  {    turnstyleController.thankyou();  }

  public void event(int event)
  {
    for (int i = 0; i < transitions.size(); i++)
    {
      Transition transition = (Transition) transitions.elementAt(i);
      if (state == transition.currentState && event == transition.event)
      {
        state = transition.newState;
        transition.action.execute();
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -