Design Patterns

Last updated Mar 5, 2002


State

กก

Object Behavioral
Design Pattern

E. Gamma, et al., Design Pattern : Elements of Reusable Object-Oriented Software, Addison-Wesley, 1995, Chapter 5, pp. 305-313


  1. Intent
  2. Also Known As
  3. Motivation
  4. Applicability
  5. Structure
  6. Participants
  7. Collaborations
  8. Consequences
  9. Implementation
  10. Sample Code
  11. Known Uses
  12. Related Patterns

Intent

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Also Known as

Objects for States

Motivation

Applicability

Use the State pattern in either of the following cases:

Structure

Participants

Collaborations

Consequences

  1. It localizes state-specific behavior and partitions behavior for different states.
  2. It makes state transitions explicit.
  3. State objects can be shared.

Implementation

  1. Who defines the state transitions?
  2. A table-based alternative.
  3. Creating and destroying State objects.
  4. Using dynamic inheritance

Sample Code

Known Uses

Related Patterns


Taewoong Jeon jeon@korea.ac.kr
Department of Computer Science, Korea University