4 ms·
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 th
by whartung 4y ago
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.