Skip to main content

State

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

Usage     Usage     Medium

UML class diagram

A visualization of the classes and objects participating in this pattern.

diagram

Sample code

This structural code demonstrates the State pattern which allows an Account to behave differently depending on its balance. The difference in behavior is delegated to State objects called RedState, SilverState and GoldState. These states represent overdrawn accounts, starter accounts, and accounts in good standing.


Output


See also