State Pattern


  • The State pattern responds to object changes and can change its behaviour by switching to a different set of operations.
  • The State pattern can be seen as a dynamic version of the Strategy pattern.

Class diagram: (see State Pattern code review)

State Pattern class diagram of sample code download


Use the State pattern when you have objects that will:

  • Change their behaviour at runtime, based on some context.
  • Are becoming complex, with many conditional branches.