- what is the state pattern?
The main idea is that, at any given moment, there’s a finite number of states which a program can be in. Within any unique state, the program behaves differently, and the program can be switched from one state to another instantaneously. However, depending on the current state, the program may or may not switch to certain other states. These switching rules, called transitions, are also finite and predetermined.
You can also apply this approach to objects. Imagine that we have a Document class. A document can be in one of three states: Draft, Moderation and Published. The publish method of the document works a little bit differently in each state:
-
In Draft, it moves the document to moderation.
-
In Moderation, it makes the document public, but only if the current user is an administrator.
-
In Published, it doesn’t do anything at all.
-
In plain words It lets you change the behavior of a class when the state changes.
-
Wikipedia says
-
The state pattern is a behavioural software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface and implementing state transitions by invoking methods defined by the pattern’s superclass. The state pattern can be interpreted as a strategy pattern that is able to switch the current strategy through invocations of methods defined in the pattern’s interface.
Then we have our Phone class that changes the state on different behavior calls
Then it can be used as follows and it will call the relevant state methods: