State Pattern

The State pattern allows an object to alter its behavior when its internal state changes. The object appears to change its class.

UML Representation


The following lists the benefits of using the State pattern:
  • Localizes state-specific behavior and partitions behavior for differentstates
  • Makes state transitions explicit

When to Use

You should use the State pattern when:
  • An object’s behavior depends on its state, and it must change its behavior at run-time depending on that state.
  • Operations have large, multipart conditional statements that depend on the object’s state.
Subpages (1): State Pattern Example