Robert Larson

  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

State Pattern using C++ (Music Player Example)

State Pattern using C++ (Music Player Example)

May 11, 2017 by Robert Larson

Table of Contents

  • What is the State Pattern?
  • State Pattern Class Diagram Explained
  • State Pattern Example
  • Example written in C++
  • Benefits of the State Pattern
  • Recommended Resources
  • In Closing

What is the State Pattern?

The definition of the State Pattern from both Design Patterns: Elements of Reusable Object-Oriented Software and Head First Design Patterns: A Brain-Friendly Guide is

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

State Pattern Class Diagram Explained

State Pattern Class Diagram

The above class diagram represents the State Pattern in its simplest form. We can see there is a Context class that has a State interface to any given concrete state class (ConcreteStateA and ConcreteStateB). Context also implements a public member Request() method that delegates its behavior to its current State. This provides a robust solution to implement different behaviors based on an object’s state. Additionally, it is well encapsulated and easily extendable if new states need to be added. Also, adding a state doesn’t require the behavior of the other states to change, thus supporting the open/closed principle.

State Pattern Example

State Pattern Music Player

To further demonstrate what the State Pattern is all about, I created a little Music Player example with some of the simplest requests and state transitions a music player requires. Consider a MusicPlayer object that acts as our Context class. It supports requests of Play(), Pause() and Stop(). It will delegate the behaviors of these requests via it MusicPlayerState member which acts as the MusicPlayer’s current state. We have three derivations of the MusicPlayerState base class. They are PlayingState, PausedState and StoppedState. Each overrides the methods of their base class that require specific behavior based on current state. For PlayingState, Pause() and Stop() are overridden. For PausedState, Play() and Stop() are overridden. Finally, StoppedState overrides just the Play() method.

Example written in C++

So we have our design finished. Let’s take a look and see what our implementation will look like.

MusicPlayer.h

MusicPlayer.cpp

MusicPlayerState.h

MusicPlayerState.cpp

PlayingState.h

PlayingState.cpp

PausedState.h

PausedState.cpp

StoppedState.h

StoppedState.cpp

MusicPlayerTest.cpp

 

Here is the output of this program.


changing from Stopped to Playing state
changing from Playing to Paused state
changing from Paused to Stopped state

All the code for this post can be found here

Benefits of the State Pattern

There are a few benefits of the State Pattern. Most noteworthy, it’s an extendable design as it allows to easily add new states without the need to change other state objects, thus supporting the open/closed principle. As a result, the pattern can potentially have many state classes. This may not be ideal but it would potentially avoid complex conditional statements if states were represented by data values. Another benefit of the State Pattern would be that state changes would be more explicit while it would be less so if states were represented by one or more data value assignments. Finally, the State Pattern may allow for State objects to be shared. This would depend on whether or not the State objects contain internal instance variables. If none exist, the object is deemed shareable.

Recommended Resources

Here are some resources I recommend for more information on the State Pattern

Head First Design Patterns: A Brain-Friendly Guide

Design Patterns: Elements of Reusable Object-Oriented Software

In Closing

There you have it. If you followed along, I hope you now have a better understanding of the State Pattern. Please feel free to leave comments and feedback. Much appreciated!

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

Posted in: c++, Design Patterns Tagged: #cplusplus, #cpp, c++, designpatterns
← Composite Pattern using C++ (Librarian Book Collection Example)
Proxy Pattern using C++ (Zoo Example) →
  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Recent Posts

  • March Madness 2019 – Weighted Random Bracket Generator
  • Gifts Ideas for Software Developers 2018-2019
  • March Madness 2018 – Second Chance Weighted Random Bracket Generator
  • March Madness 2018 – Weighted Random Bracket Generator
  • Builder Pattern using C++ (NFL Scheduler Example)

Archives

  • March 2019
  • November 2018
  • March 2018
  • August 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017

Follow me on Twitter

My Tweets

Copyright © 2025 Robert Larson.

Grace WordPress Theme by SiteChurch

%d