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.
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. Read More