Pattern:
State


Author

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

Intent

"Allow an object to alter its behavior when its internal state changes. The object will appear to change its class." (Gamma, E., R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Reading, MA: Addison-Wesley, 1995)

Motivation

Consider a class TCPConnection (Context in the thumbnail) that represents a network connection. A TCPConnection object can be in one of several different states: Established, Listening, Closed. When a TCPConnection object receives requests from other objects, it responds differently depending on its current state. For example, the effect of an Open request depends on whether the connection is in its Closed state or its Established state. The State pattern describes how TCPConnection can exhibit different behavior in each state. The key idea in this pattern is to introduce an abstract class called TCPState (State in the thumbnail) to represent the states of the network connection. The TCPState class declares an interface common to all classes that represent different operational states. Subclasses of TCPState implement state-specific behavior. For example, the classes TCPEstablished (ConcreteState in the thumbnail) and TCPClosed (ConcreteState in the thumbnail) implement behavior particular to the Established and Closed states of TCPConnection. Connection delegates all state-specific requests to this state object. TCPConnection uses its TCPState subclass instance to perform operations particular to the state of the connection. Whenever the connection changes state, the TCPConnection object changes the state object it uses. When the connection goes from established to closed, for example, TCPConnection will replace its TCPEstablished instance with a TCPClosed instance.

Known Uses

This pattern has been used on the following systems: The State pattern and its application to TCP connection protocols are characterized in: Johnson, R.E. and J. Zweig. Delegation in C++. Journal of Object-Oriented Programming, 4(11):22-35, November 1991. "Tools" for performing operations by direct manipulation are provided by many interactive drawing programs (i.e. A user may click and drag using a line-drawing tool to create a new line). The editors behavior changes. The State pattern can be used to change the editors behavior depending on the current tool. This technique is used by HotDraw (Johnson, R. Documenting frameworks using patterns. In Object-Oriented Programming Systems, Languages, and Applications Conference Proceedings, pp. 63-76, Vancouver, British Columbia, Canada, October 1992. ACM Press.) and Unidraw (Vlissides, J.M., and M.A. Linton. Unidraw: A framework for building domain-specific graphical editors. ACM Transactions on Information Systems, 8(3):237-268, July 1990.)

See Also

Flyweight and Singleton patterns

Thumbnail

Keywords

Singleton pattern, Flyweight pattern, State pattern, behavioral patterns, Gamma patterns, Objects for States pattern, state, encapsulation (object states)

Business Domains

any system requiring dynamic behavioral assignments to objects

Problem Forces

need to break up many behaviors with multiple conditionsneed to modify object behaviors dynamically without affecting existing objects

Benefits

localizes state-specific behaviorpartitions behavior for various states

Implementation Files

Test driver Test.javaSimple implementation of the pattern - State class. State.javaSimple implementation of the pattern - Context class. Context.javaSimple implementation of the pattern - ConcreteStateB class. ConcreteStateB.javaSimple implementation of the pattern - ConcreteStateA class. ConcreteStateA.javaTest driver teststate.cppSimple implementation of the pattern. state.h



Generated on Fri Oct 20 11:21:43 GMT+02:00 2000 by Framework Studio