This article explains State design pattern in java with UML class diagram. It then takes an example scenario in java and explains it with class diagram and code.
Introduction: State Design Pattern is a behavioral design pattern among the Gang Of Four(GOF) Design PatternsArticle on GOF Patterns & their types. Being a behavioral design pattern, the State pattern deals with how the objects communicate and share responsibilities among each other.
What is State Design Pattern
State Design Pattern allows the behavior of an object to vary based on its state. I.e. whenever the object’s state changes, its behavior changes as per its new state. To the observer it appears as if the object has changed its class.
Important advantage of State Design Pattern
If there are state-based scenarios in a system where the different states of an object are implemented via multiple sets of conditional statements. And each set of conditional statements is applied based on the condition that the object is in the state corresponding to those statements. In such scenarios, instead of writing multiple if-else blocks for each of the states and juggling with the various state dependent values, it becomes much easier to encapsulate each of these set of statements into separate classes of their own. The implementation of each state can thus vary independently of the other states.
Class Diagram for State Design Pattern
Explanation of State Design Pattern’s Class Diagram
Stateis the base class for all the child
Contextclass holds a reference to the
Statebase class in an attribute named
state. In this attribute
Contextstores the object of specific
ConcreteStateimplementation corresponding to the state in which the
Clientinvokes a request on the
Contextclass using its
- When the
handle()method is invoked on the base state reference stored in the
Contextthen the overloaded
handle()method of the current
ConcreteStateinstance stored in it is executed.
- Hence, the states show different behavior for the same
handle()method invoked on the
Contextbased on the current state the system/context is in.
State Design Pattern in Java – Class Diagram:
The Java class diagram above depicts State Design pattern implemented for a ChangeRequest State Handler. Lets quickly go through whats there in UML class diagram & corresponding code –
Stateis the base interface which implements 3 methods –
markTested(). These 3 methods represent the 3 transitions possible between states.
- There are 4 concrete states –
- The allowed transitions between states are –
UnderDevStatetransition is possible through
UnderTeststate transition is possible through
UnderTeststate tester can transition the CR back to
UnderDevstate if he finds an error using
assignToDev(). If tester passes the CR then he transitions it to
Clientinvokes series of transitions in sequence. Some transitions are valid and move the CR to the next state. For eg: When moving from New to UnderDev the following is output – Assigning to available developer.
- There are also some invalid transitions such as invoking
NewStatewithout going through
UnderDevstate in which case the line Cannot be assigned to tester from new state – is printed.
- Thus, a CR moves through the states – New > UnderDev > UnderTest > Closed during its lifecycle.
In the above tutorial we understood what is State design pattern and its main advantage. We then looked at the UML class diagram for State Design Pattern & its explanation, a Java Use Case implementing State pattern with its class diagram and code for the classes shown in the class diagram, followed by explanation of both the class diagram & code. This concludes the tutorial on State design pattern.