Skip to content

Instantly share code, notes, and snippets.

@chrisdew
Created July 24, 2013 08:05
Show Gist options
  • Save chrisdew/6068800 to your computer and use it in GitHub Desktop.
Save chrisdew/6068800 to your computer and use it in GitHub Desktop.
enum state machine
interface IState {
public void GuiBookOn() throws InvalidStateException;
public void GuiBookOff() throws InvalidStateException;
public void NetBookedOn() throws InvalidStateException;
public void NetBookedOff() throws InvalidStateException;
}
public enum State implements IState {
BOOKED_OFF {
@Override
public void GuiBookOn() throws InvalidStateException {
// send book on message
changeState(BOOKING_ON);
}
}, BOOKING_ON {
@Override
public void NetBookedOn() throws InvalidStateException {
changeState(DESKTOP);
}
}, DESKTOP {
@Override
public void GuiBookOn() throws InvalidStateException {
// send book off message
changeState(BOOKING_OFF);
}
}, BOOKING_OFF {
@Override
public void NetBookedOn() throws InvalidStateException{
changeState(BOOKED_OFF);
}
};
public static State state = BOOKED_OFF;
private static void changeState(State newState) {
// inform listeners, maybe emit intents?
state = newState;
}
public void GuiBookOn() throws InvalidStateException { throw new InvalidStateException(); };
public void GuiBookOff() throws InvalidStateException { throw new InvalidStateException(); };
public void NetBookedOn() throws InvalidStateException { throw new InvalidStateException(); };
public void NetBookedOff() throws InvalidStateException { throw new InvalidStateException(); };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment