Design Patterns State Pattern - Design Patterns

What is Design Patterns State Pattern?

In State pattern a class behaviour changes based on its state. This kind of design pattern comes under behaviour pattern.

In State pattern, we create objects which stand for different states and a context object whose behaviour is different as its state object alteration.

Implementation

We are going to make a State interface important an action and concrete state classes implementing the State interface. Framework is a class which carries a State.

StatePatternDemo, our presentation class, will use Context and state items to demonstrate modify in Context behaviour based on type of state it is in.
state_pattern_uml_diagram

Step 1

Create an interface.

State.java

Step 2

Create concrete classes implementing the similar interface.

StartState.java

StopState.java

Step 3

Create Context Class.

Context.java

Step 4

Use the framework to see modify in behaviour when State changes.

StatePatternDemo.java

Step 5

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics