# Design patterns: State pattern

You probably know that a pressure cooker cannot be opened when it is very hot with plenty of steam built up inside. If you yank at the weight, it will protest with a loud hiss. But there are no problems opening the same cooker either before cooking or after it has completely cooled down. How can the same apparatus behave differently under different conditions for the same procedure: opening the lid? Software engineers will say that the cooker is using the state design pattern.

# State pattern in real life

In school, we were taught about how a litmus paper changes colour when exposed to a substance which is either alkaline or acidic. For our experiment today, let us take a red litmus paper. Solution A is lemon juice and solution B is baking soda in water.

When we dip the red litmus paper inside solution A, nothing happens. The red colour is retained. But dipping the same paper inside solution B changes the paper to blue colour. We dip the now blue litmus paper again in solution B. But nothing changes. However dipping it into solution A turns the colour to red.

Neither the blue litmus, nor the red litmus changes colour when dipped in a solution C, which is salt water.

The above behaviour exemplifies a state pattern in action. Both the litmus papers have fixed states: acidic or alkaline. An acidic paper is red in colour and changes colour ONLY when exposed to an alkaline substance. The blue alkaline paper changes colour ONLY when exposed to an acid. Neither reacts to a neutral substance like salt or to a substance which is already in the same state as the paper. The same three activities: exposing the paper to acid, salt and alkali, yields different results depending on the state of the paper.

We can find forms of state pattern, even in nature. A raw mango tastes sour, while a ripe mango tastes sweet. A caterpillar cannot fly, but a butterfly can.

# So, what is state pattern?

State pattern represents the various states that an object can go through. The same operations will lead to different behaviour in these different states.

In our litmus paper example, the litmus paper has two states: Acidic and Alkaline. A litmus paper is capable of getting dipped in an acid, neutral or alkaline solution. These are the methods available in each state. The behaviour of each method is most likely different in each state. A base state is first created to be used as a template by different states. This is done with an interface. Next, methods that will exhibit a different behaviour in each state are declared in the interface. These methods will be used to define the behaviour of each state. Each state is a class that implements the interface, i.e. uses the interface as a template. The litmus paper, which can undergo these three states is called the Context class. The context class is directly seen by the user, whereas the presence of the states is just conceptual to him/her.

If you do not understand interfaces, classes, objects and methods, I suggest a refresher atÂ Introduction to object-oriented software. Here is a diagram of the state pattern.

# State pattern in software

Vending machines and ATMs are often rife with different states. On normal days, they will work as advertised. However, cash can be low or an ATM can be out of service. In such cases, the ATM machine will display its appropriate state to the user. You will see notifications such as ‘Insufficient cash’ or ‘Out of service’, when you try to operate such as machine. Washing machine software will make sure that the loader’s door is fully closed before washing or spinning. An open door will trip the system and cause beeps, if you try to start a washing session.

# Conclusion

In both real and digital life, the various parts of a system are constantly undergoing change. These changes often need a change in behaviour for the system to work efficiently. State design pattern helps maintain several states on the same object, while keeping the core of that object intact.

[subscribe_form]