📄 turnstyle.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 + -