AlgoViz

State pattern

medium 55 min

The State pattern lets an object change its behaviour when its internal state changes.

Watch lesson4:37

Where it shows up

The same pattern appears across everyday software.

  • TCP

    RFC 9293 defines a connection by its states: LISTEN, SYN-SENT, SYN-RECEIVED, ESTABLISHED, FIN-WAIT-1 and others up to CLOSED. Every segment and every user call is described state by state, the biggest state and event table you use each day.

  • Android MediaPlayer

    The reference says that playback control is managed as a state machine and draws it. Calling a method such as prepare in a wrong state throws IllegalStateException: refuse with a reason.

  • Spring Statemachine

    A framework for state machines in Spring applications. Its reference has chapters on guards, actions, hierarchical states, timers, listeners and persisting a machine: the topics of the FAANG pages.

  • XState

    State machines, statecharts and the actor model for JavaScript and TypeScript. Its core package includes graph traversal and utilities for model-based testing.

  • AASM for Ruby

    Adds states, events, guards and callbacks to any Ruby class, with adapters for several object-relational mappers, and an option for pessimistic locking of the record during a transition.

  • Qt State Machine

    QStateMachine is hierarchical, based on Harel's statecharts, and its execution algorithm follows SCXML. States can contain states and run side by side.

  • AWS Step Functions

    Workflows are state machines written as data. For Standard workflows, starting an execution twice with the same name and input is treated as one start while it runs.

  • Stripe PaymentIntent

    A payment moves through statuses such as requires_payment_method, requires_confirmation, requires_action, processing and succeeded. Client code decides what to show from the status.

  • Unity Animator

    Characters are animated by state machines: each state plays a clip, transitions have conditions, and the manual explains that the machine is in one state at a time.

  • Akka typed actors

    An actor can model a finite state machine: each state is a behavior, and handling a message returns the behavior for the next one. An actor handles one message at a time, which gives run to completion.