Skip to content
AyoKoding

Overview

Want to master Finite State Machines through practical examples? This by-example guide teaches FSM through annotated code and diagram examples in Java, Kotlin, C#, and TypeScript, organized by complexity level and using a shared Procure-to-Pay (P2P) domain so every example builds on the same problem.

Domain Context

All examples model the procurement-platform-be backend. Employees request goods, managers approve, suppliers fulfill, and finance pays. This single domain thread — rather than a new toy problem per example — lets you compare FSM patterns across levels without re-learning the context.

Learning Path

Three progressive levels, each adding a new aggregate. Java is the canonical implementation; Kotlin, C#, and TypeScript variants appear throughout to show how the same FSM patterns map onto different type systems:

  • BeginnerPurchaseOrder state machine: states as sealed types / discriminated unions, transitions as pure functions, guard conditions, invalid-transition rejection — in Java, Kotlin, C#, and TypeScript.
  • Intermediate — adds Invoice state machine: three-way match guards, state-entry/exit actions, XState-style library usage (TypeScript), FSM as protocol enforcement — all four languages.
  • Advanced — adds Supplier lifecycle and Payment state machine: hierarchical states, parallel regions, history states, FSM persistence and event-sourcing intersection, statecharts — all four languages.

Where Go and C Fit

FSM is the architecture topic with the widest paradigm reach. Beyond the OOP and FP idioms taught on this and the sibling FP track, two further idioms have first-class canonical literature:

  • Go: state-as-field switch dispatch + the looplab/fsm library (3.4k stars, Apache 2.0, actively maintained). State is an int or string field; transitions are dispatched via switch; the library adds event-table configuration (Event{Name, Src []string, Dst string}) with callback hooks. Runtime-checked, not compile-time.
  • C: the function-pointer table idiom — a 2D array action[state][event] of function pointers; each cell is the transition action. Miro Samek's Practical UML Statecharts in C/C++ (Routledge, 2nd ed. 2008) is the authoritative book-length treatment, including the QP/C active-object framework for hierarchical state machines on embedded systems. The Linux kernel TCP and USB state machines are real-world examples of this idiom at production scale.
  • Rust: the typestate idiom — each state is a distinct struct type, transitions are functions consuming self and returning a different state type, and ownership makes illegal transitions fail to compile. Canonical sources: Pretty State Machine Patterns in Rust — Hoverbear; Type-Driven API Design in Rust — Will Crichton. Strictly stronger compile-time guarantee than the FP enum-based formulation taught on the sibling FP track.

The OOP idioms taught here (state as enum or sealed type field, transitions as aggregate methods, XState-style declarative config in TypeScript) translate to all three with adjustments — but Go, C, and Rust each have a canonical native idiom that does not collapse to the OOP form. The in-procedural-by-example track teaches those three native idioms side by side.

Examples by Level

Beginner (Examples 1–25)

Intermediate (Examples 26–50)

Advanced (Examples 51–75)

Structure of Each Example

Every example follows a five-part format:

  1. Brief Explanation — what FSM concept the example demonstrates (2-3 sentences)
  2. State Diagram — Mermaid stateDiagram-v2 with accessible color palette
  3. Annotated Code — Java (canonical), Kotlin, C#, and TypeScript implementations with 1.0-2.25 comment lines per code line; language tabs appear where the idiom differs meaningfully
  4. Key Takeaway — the core principle to retain (1-2 sentences)
  5. Why It Matters — design rationale and consequences (50-100 words)

Last updated January 30, 2026

Command Palette

Search for a command to run...