Definition
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.. (behavior of class is implemented in State class, change state make behavior change too)
UML Class Diagram
State: when and where use it
- The state of an object is represented by the values of its instance variables. A client can change the state of an object by making property or method calls which in turn change the instance variables. These types of objects are called stateful objects. State is frequently a core concept in complex systems, such as, stock market trading systems, purchasing and requisition systems, document management systems, and especially work-flow system.
- The complexity may spread to numerous classes and to contain this complexity you use the State design pattern. In this pattern you encapsulate state specific behavior in a group of related classes each of which represents a different state. This approach reduces the need for intricate and hard-to-trace conditional if and case statements relying instead on polymorphism to implement the correct functionality of a required state transition.
- The goal of the State design pattern is to contain state-specific logic in a limited set of objects in which each object represents a particular state. State transition diagrams (also called state machines) are very helpful in modeling these complex systems. The State pattern simplifies programming by distributing the response to a state transition to a limited set of classes in which each one is a representation of a system's state.
Sample
- Context
- State
- Concrete state
Ref: Gang of Four tutorial
No comments:
Post a Comment