> Design Patterns > State Design Pattern

State Design Pattern

State design pattern is used to change the behavior of object based on current state of object.

Intent

  • Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

The intent of state design pattern is to change the behavior of the object as its state changes, and have the behavior reflected through classes.

Problem

  • Object has different states and has to respond differently based on its current state.

Handling of changes of behavior, based on state changes has to be run-time and using conditions for it will make the design complex and inflexible.

Solution

  • Have a separate abstract class for State, derive concrete class for each state and do the operation as required in overridden methods.

Thus a separate class is required for different state, so that appropriate state object can be used based on the current state of object.

Where state design pattern is applicable?

  • An object has to change its behavior at run-time based on its current state.
  • Lot of conditional statements are used based on the state of object to do the operations.

State Design Pattern UML Structure

State Design Pattern UML Structure

Participant classes of state design pattern

  • Context class provides the interface to client. It has instance of State object.
  • State class provides interface Handle() for behavior.
  • Concrete classes ConcreteStateA and ConcreteStateB represent different states and implement the method Handle(), which does the operations based on current state.

How they work together?

  • Client uses the context method Request() to do the operation, context uses the method Handle() with appropriate concrete state object, based on its current state. ConcreteState object method Handle() does the operation applicable for that state. The transition of state can be done in concrete State object or context.
  • So the appropriate behavior has been chosen at run time, using concrete state object based on current state of object. This also provides flexibility to add new state object and makes it easy for maintenance as conditional statements would have increased the complexity as the number of states grow. Also it localizes the operation for behavior too, so it can be easily modified.

Example

Here is the state design pattern example.

Source code submitted -> Build Started -> Build Completed -> Regression Test Suite Started -> Tests Execution Completed -> Notified To Developers. State is getting changed, so change the behavior to do the operation applicable to that.

Implementation Code

C++ State Pattern in C++ State Pattern C++ Example
C# State Pattern in C# State Pattern C# Example
Java State Pattern in Java State Pattern Java Example
Python State Pattern in Python State Pattern Python Example
JavaScript State Pattern in JavaScript State Pattern JavaScript Example
PHP State Pattern in PHP State Pattern PHP Example
Ruby State Pattern in Ruby State Pattern Ruby Example
Swift State Pattern in Swift State Pattern Swift Example
Objective-C State Pattern in Objective-C State Pattern Objective-C Example


Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books C In Depth, Data Structures Through C In Depth. He has 18+ years experience in industry and worked on architecture and design of multiple products. This article State Design Pattern is from his Design Patterns course.