Key Words: State design pattern
Topics at a glance:
- Delegating state manipulation using state objects.
- Let us turn on a bulb!
State design pattern
In this chapter we will see another behavioral pattern – the state pattern. We have all heard about Finite State Machines (FSM). FSM abstracts the behavior of the machine into identifiable unique entities called states. At any point of time, an FSM will be in a unique state and will exhibit behavior appropriate to that state. FSM can change from one state to another state upon the trigger of a specific event/condition.
In state pattern, we basically split the state and the machine. It is more correct to say that a machine is at a state rather than a machine has a state. In state pattern we make a State class as a base class and make each state the machine support into separate derived classes. Machine as said before, is always at a state in any given context. That said, a machine here, can be treated as a context class which will be at a state at any point of time. Machine will have a reference (pointer) to its unique state object that identifies/realizes its actual state, in that context. Machine will implement certain interfaces to manipulate its state for the clients. Machine will delegate the actual state manipulation to the state object. State object instance manages the state by executing the set of actions required to be performed in that state and also implements the necessary functionality to transition to another state as required.
For demonstrating an actual state pattern implementation, I am taking an example of a light bulb that can be turned on and off. Here the machine is defined by the Bulb class and it’s identifiable states are On_State class and Off_State class; State class being the abstract base class that machine will use to manipulate its state.
Let us see this in action.
Want to see the result?
Bulb controller application What to do next : ON, OFF, EXIT ON Bulb is switched on What to do next : ON, OFF, EXIT OFF Bulb is switched off What to do next : ON, OFF, EXIT OFF already in off state What to do next : ON, OFF, EXIT ON Bulb is switched on What to do next : ON, OFF, EXIT ON already in on state What to do next : ON, OFF, EXIT OFF Bulb is switched off What to do next : ON, OFF, EXIT EXIT Exiting bulb controller application
Here, one thing to note is that, when the bulb is in ON state, a command to switch it on, will result in an error. Similarly, when bulb is in OFF state, you cannot turn it off again. i.e. On_State will only define the functionality to transition to OFF state, whereas Off_State defines the functionality to transition to ON state. Machine itself doesn’t need to track in which state it is currently at, in its course of execution. That said, using state pattern, we have separated the state and the machine.
Please go through the next chapter, which will improvise this Bulb controller application using bridge pattern.
Enjoyed the chapter? Let me know in the comments below. Thanks 😊