Update:
State pattern might a wrong way to solve this. Hence, any other pattern is welcome. Basically I'm looking for a way to have guard conditions for each state yet having a clean and maintainable code. How would front-end side routing systems like, emberjs, ui-router and react-router implement guard conditions
to avoid entering a specific state if the condition is not met?
I want to implement a finite state machine by using State Pattern but I can't wrap my head around it. In short it's like:
If error -> error state
If A && B && C -> second state
If only A -> first state
At any state, on error, we go to error state. inputs (events) A, B and C might arrive at any order but if they all pass we go to 2nd state. If only input A applies, then we go to 1st state.
The following state diagram is taken from Martin Fowler's Domain Specific Language book.
In the description he says:
Miss Grant, has a secret compartment in her bedroom that is normally locked and concealed. To open it, she has to close the door, then open the second drawer in her chest and turn her bedside light on in either order. Once these are done, the secret panel is unlocked for her to open.
I emphasize, that turning light
and opening 2nd drawer
can happen in either order. Same as A, B and C.
Based on @SQLPolice comment and the book, I have drawn this:
But the problem is, I might have (A && B && C && D && D && E). In that case it's gonna be cumbersome to have all of the combination interim states.
lex
,flex
, etc. (for lexical analysis). They create C code with a lot of labels andgoto
jumps ... – Wenwenceslaus