State Pattern
General Description
• A type of Behavioral pattern.
• Allows an object to alter its behavior when its
internal state changes. The object will appear
to change its class.
• Uses Polymorphism to define different
behaviors for different states of an object.
A Gumball Machine
• A gumball machine is a type of
vending machine which
dispenses gumballs covered in
brightly colored candy coatings
• To operate a gumball machine,
a consumer inserts a coin and
turns a knob, which allows a
gumball or a gumball
assortment to drop out
State Machine of Gumball Machine
Implementing a State Machine - Step 1
• Gather up the states
Implementing a State Machine - Step 3
• Gather all the events that can happen
• These events are the Gumball’s interface. The
things that can be done by the user
Implementing a State Machine - Step 2
• Create an instance variable to hold the current
state
• Define values for each of the state
Implementing a State Machine - Step 4
• Write a class that represents the State
Machine or the context object
Implementing a State Machine - Step 4
Implementing a State Machine - Step 4
A Change Request!
• The updated State Machine?
Changes in Code
• Difficult to maintain
– Back to basics
– Design Principles
Redesign the Gumball Machine
• Define a State interface that contains a method
for every action in the Gumball Machine
• Implement a State class for every state of the
machine.
– These classes will be responsible for the behavior of
the machine when it is in the corresponding state
• Get rid of all the conditional code and delegate
the work to the state class
The New Design
When to use STATE pattern ?
• State pattern is useful when if (myself = happy) then
there is an object that can be {
in one of several states, with eatIceCream();
different behavior in each ….
state. }
else if (myself = sad) then
• To simplify operations that {
have large conditional goToSleep();
statements that depend on
….
the object’s state.
}
else if (myself = ecstatic) then
{
….
Example I
water StateOfWater
state variable increaseTemp()
decreaseTemp()
increaseTemp()
decreaseTemp()
WaterVapor LiquidWater Ice
Client increaseTemp() increaseTemp() increaseTemp()
increaseTemp() decreaseTemp() decreaseTemp() decreaseTemp()
How is STATE pattern
implemented ?
•“Context” class:
Represents the interface to the outside world.
•“State” abstract class:
Base class which defines the different states of
the “state machine”.
•“Derived” classes from the State class:
Defines the true nature of the state that the
state machine can be in.
Context class maintains a pointer to the current state.
To change the state of the state machine, the pointer
needs to be changed.
Example II
MyMood MoodState
state variable
doSomething()
mad angry happy
Client
doSomething()
doSomething() doSomething() doSomething()
Benefits of using STATE pattern
• Localizes all behavior associated with a particular state into one object.
New state and transitions can be added easily by defining new subclasses.
Simplifies maintenance.
• It makes state transitions explicit.
Separate objects for separate states makes transition explicit rather than using
internal data values to define transitions in one combined object.
• State objects can be shared.
Context can share State objects if there are no instance variables.
Food for thought…
• To have a monolithic single class or many subclasses ?
Increases the number of classes and is less compact.
Avoids large conditional statements.
• Where to define the state transitions ?
If criteria is fixed, transition can be defined in the context.
More flexible if transition is specified in the State subclass.
Introduces dependencies between subclasses.
• Whether to create State objects as and when required or to create-them-
once-and-use-many-times ?
First is desirable if the context changes state infrequently.
Later is desirable if the context changes state frequently.