Thursday 6 August 2015

The State Pattern

The State Pattern


Defined

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

Encapsulates state-based behavior and delegate behavior to current state

Diagrams



Code Examples

 


 


Bullet Points

  • The State Pattern allows an object to have many different behaviors that are base on its internal state.
  •  Unlike a procedural state machine, the State Pattern represents state as a full blown class.
  • The Context gets its behavior by delegating to the current state object it is composed with.
  • By encapsulating each state into a class, we localize any changes that will need to be made.
  • The State and Strategy Patterns have the same class diagram, but they differ in intent.
  • Strategy Patterns typically configures Context classes with a behavior or algorithm.
  • State Pattern allows a Context to change its behavior as the state of the Context changes.
  • State transitions can be controlled by the State classes  or by the context classes.
  • Using the State patter will typically result in a great number of classes in your design.
  • State classes may be shared among Context instances.

No comments:

Post a Comment