State pattern

This can be a cleaner way for an object to change its behavior at runtime without resorting to conditional statements and thus improve maintainability.

Such problems cover the design of flexible and reusable object-oriented software, such as objects that are easy to implement, change, test, and reuse.

In this, the pattern describes two solutions: This makes a class independent of how state-specific behavior is implemented.

Instead, Context refers to the State interface for performing state-specific behavior (state.handle()), which makes Context independent of how state-specific behavior is implemented.

The next time, Context again calls handle(this) on its current state object (ConcreteStateB), which performs the operation and changes context's current state to ConcreteStateA.

A sample UML class and sequence diagram for the State design pattern. [ 2 ]
State in UML [ 1 ]