State Pattern

This lesson discusses how an object exhibits very different behavior and appears to be an instance of a different class when its internal state changes under the state pattern.

What is it ?

The state pattern will be reminiscent of automata class from your undergraduate degree as it involves state transitions for an object. The state pattern encapsulates the various states a machine can be in. The machine or the context, as it is called in pattern-speak, can have actions taken on it that propel it into different states. Without the use of the pattern, the code becomes inflexible and littered with if-else conditionals.

Formally, the pattern is defined as allowing an object to alter behavior when its internal state changes so that it appears to change its class.

Get hands-on with 1400+ tech skills courses.