State Design Pattern
The State design motif
The informal description: Excerpts from [Gamma et al. 1995] (adapted for
Intent: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Structure: Original OMT diagram converted to UML (Why
- Context: defines the interface of interest to clients, and maintains an instance of a ConcreteState subclass that defines the current state.
- State: defines an interface for encapsulating the behavior associated with a particular state of the Context.
- ConcreteState subclasses: each subclass implements a behavior associated with a state of the Context.
- Context delegates state-specific requests to the current ConcreteState object.
- A context may pass itself as an argument to the State object handling the request. This lets the State object access the context if necessary.
- Context is the primary interface for clients. Clients can configure a context with State objects. Once a context is configured, its clients don't have to deal with the State objects directly.
- Either Context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances.
|State pattern in LePUS3 (legend)
|State pattern in Class-Z (legend)