State Design Pattern

What is the State Pattern

The State Pattern is a behavioral design pattern that is used when an Object changes its behavior on its internal state.

  • Within any unique state a program can behave differently, and a program can be switched from one state to another state instantaneously!
  • Remember the Infinite State Machine Problem in my C++ Projects

Example Using Music Player

In the PlayingState Class, which inherits from the MusicPlayerState, there is two functions/methods that can be invoked which are Stop and Pause!

In the StopState Class, you can only use the Play method!! Makes sense, you can’t go from Stop to Pause!