Re:Facts about State Machines
by whartung on Saturday October 01, 2022 @12:41AM
(#33043938)
I’ve always been fond of this pattern.
state = state.next(context, data);
Context just holds the global stuff of the process, data is the new input into the machine and state is the current state.
A simple example is an SMTP server. State is the current command (or idle), context is email message being built, and data is the line read from the socket.
Context context = new Context();
sendSMTPBanner();
State state = new IdleState();
while (!state.isFinished) {
String data = readLine();
state = state.next(context, data);
}
A simple example is an SMTP server. State is the current command (or idle), context is email message being built, and data is the line read from the socket.
Conceptually pretty simple.