In State pattern, a class behavior changes based on its state. This type of design pattern comes under behavior patternBullet Three.
In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes
Implementation
We are going to create a State interface defining an action and concrete state classes implementing the State interface. Context is a class which carries a State.
StatePatternDemo, our demo class, will use Context and state objects to demonstrate a change in Context behavior based on the type of state it is in
Example
Step 1
Create an interface.
State Pattern
State Pattern
Step 2
Create concrete classes implementing the same interface.
State Pattern
Step 4
Use the Context to see a change in behavior when State changes.