State pattern


The state pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. This pattern is close to the concept of finite-state machines. The state pattern can be interpreted as a strategy pattern, which is able to switch a strategy through invocations of methods defined in the pattern's interface.
The state pattern is used in computer programming to encapsulate varying behavior for the same object, based on its internal state. This can be a cleaner way for an object to change its behavior at runtime without resorting to conditional statements and thus improve maintainability.

Overview

The state design pattern is one of twenty-three design patterns documented by the Gang of Four that describe how to solve recurring design problems. Such problems cover the design of flexible and reusable object-oriented software, such as objects that are easy to implement, change, test, and reuse.
The state pattern is set to solve two main problems:
Implementing state-specific behavior directly within a class is inflexible because it commits the class to a particular behavior and makes it impossible to add a new state or change the behavior of an existing state later independently from the class. In this, the pattern describes two solutions:
This makes a class independent of how state-specific behavior is implemented. New states can be added by defining new state classes. A class can change its behavior at run-time by changing its current state object.

Structure

In the above Unified Modeling Language class diagram, the Context class doesn't implement state-specific behavior directly. Instead, Context refers to the State interface for performing state-specific behavior, which makes Context independent of how state-specific behavior is implemented. The State1 and State2 classes implement the State interface, that is, implement the state-specific behavior for each state. The UML sequence diagram shows the run-time interactions:
The Context object delegates state-specific behavior to different State objects. First, Context calls operation on its current state object, which performs the operation and calls setState on Context to change context's current state to State2. The next time, Context again calls operation on its current state object, which performs the operation and changes context's current state to State1.

Example

Java

The state interface and two implementations. The state's method has a reference to the context object and is able to change its state.

interface State
class LowerCaseState implements State
class MultipleUpperCaseState implements State

The context class has a state variable that it instantiates in an initial state, in this case LowerCaseState. In its method, it uses the corresponding methods of the state object.

class StateContext

The demonstration below shows the usage:

public class StateDemo

With the above code, the output of main from StateDemo is:

monday
TUESDAY
WEDNESDAY
thursday
FRIDAY
SATURDAY
sunday