State Design Pattern in Java

State pattern comes under behavior design pattern in JEE. The State pattern allows an object to alter its behavior when its internal state changes. State pattern is used to provide a systematic and lose-coupled way to achieve this through Context and State implementations. Context is the class that has a state reference to one of the concrete implementations of the state and forwards the request to the state object for processing. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements such as using switch-case or if-else statements.

The original definition of State Pattern in Gang of Four Book is given below

Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

When should we use the State Pattern

We should use the State pattern when the behavior of an object should be influenced by it’s internal state.

Example

A TV can be switched ON or OFF. So TV is either in ON or OFF state. A person can be in HAPPY or in EMOTIONAL state.

Implementation

Let’s look at an example of the state pattern. Firstly, we will define the State interface. It declares one method, doAction().

The StartState class is a Concrete State that implements doAction() of State. This displays a message when a car is started.

The StartState class is a Concrete State that implements doAction() of State. This displays a message when a car is started.

The MovingState class is a Concrete State that implements doAction() of State. This displays a message when a car is moving after started.

The StopState class is a Concrete State that implements doAction() of State. This displays a message when a car is stopped after started.

The Car class is the Context class. It contains an State reference to a concrete state. In this example, we have Car implements the State reference, and we pass the calls to Car’s doAction() method on to the corresponding method on the state reference. As a result of this, a Car object behaves differently depending on the state of Car (i.e., the current State reference, the car can be in different states such as start, moving, stop).

The StatePatternTest class demonstrates the state pattern. First, it creates a Car object with a StartState object. We display the results of doAction() when the car object is in the start state. Next, we change the car object’s state with a MovingState object. We display the result of doAction(), and we see that in the moving state, the car object’s behavior is different. Again we change the car object’s state to a StopState and we display the result of doAction().

The console output as shown below

Benefits of State Pattern

The benefits of using State pattern to implement polymorphic behavior is clearly visible, the chances of error are less and it is very easy to add more states for additional behavior making it more robust, easily maintainable and flexible. Also State pattern helped in avoiding if-else or switch-case conditional logic in this scenario.

That’s all. Thank you for your reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.