In a manufacturing environment, for a specific process, there are fairly straightforward C# Winforms Desktop applications that involve enum-based state machines running on infinite loop, following a structure similar to below (state names kept general for the sake of example).
switch(state):
case STATE0:
// code (ui update, business logic, and/or database/device communication)
if (...)
state = STATE1; // goes to next state
break;
case STATE1:
// code (ui update, business logic, and/or database/device communication)
if(...)
state = STATE2; // goes to next state
break;
case STATE2:
// code (ui update, business logic, and/or database/device communication)
if(...)
state = STATE1; // goes back to STATE1
else if (...)
state = STATE3; // goes to next state
break;
case STATE3:
// code (ui update, business logic, and/or database/device communication)
if (...)
state = STATE0; // goes back to STATE0
break;
There are many products that go through this process. There are many states across products that are almost the exact the same (e.g. state0). But product-specific logic within the states are slightly different across products.
Is there a way to refactor the above switch statement to a more cleaner, flexible finite state machine, that can account for variation within the states?