patterns overview state pattern decorator pattern state and decorator patterns downloads references home
about three-ten software created with contact
State Pattern: Overview  

Overview

The state design pattern is a behavioural pattern that allows the programmer to implement state changes, much like those in a finite state machine easily. Using the state design pattern will allow the object to change its behaviour depending on it's internal state at run-time. Therefore, the object will remain the same, but how it behaves is now different.

Uses

There are many possible uses for this design pattern.

  1. Changing the state of a file between Read Only/Read and Write
  2. Changing the status of a account (bank, video store, library) depending on a factor (balance, unreturned books/videos etc)
  3. Changing the emotion of a character in a game between happy/sad/angry etc.

Benefits

Easier to implement as compared to say, using case or conditional statements to switch between different states.

Disadvantages

It is difficult to implement non-deterministic state machines.

Related Patterns

There are four patterns that are related to the state pattern. These are the flyweight pattern, the mediator pattern, the objectifier pattern, and the singleton pattern.

  • The state pattern is a form of the objectifier pattern
  • The flyweight pattern can be used to share different state object.
  • Often, the state pattern is paired with the mediator pattern when creating user interfaces.
  • The singleton pattern allows you to implement states that are nonparametric.