News for hackers. Stuff that matters.
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);
  }
Conceptually pretty simple.

There are no comments on this comment yet.