Event Programming – Relationship Between Event-Driven Design and State Machines

event-programmingfinite-state machine

I'm trying to understand event driven design better and have not picked up a book on it yet. But just from others explaining it to me it seems to be really similar to state machines/charts. What are the similarities and differences between the two?

Best Answer

Events represent change of state.

If the state is your age in years, your birthday is the event that changes it.

If you model a state machine with a graph, the various states are represented by nodes. Every edge that connects two nodes represents a change of state.

Whether that means that every edge should actually be modeled as an event in code depends on the choice of states and your goals.

If the age state machine represented every year as an individual state, an event might model different aspects of the graph such as

  • generic state transition: "birthday"-event
  • specific state transitions: "becoming an adult"-event

Events are often modeled to carry auxiliary data, such as what state transition they represent: "got X amount of time older"-event