Design Patterns with PlantUML: State Pattern

This post gives a brief overview about the State Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML.

The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.

What is the State Pattern?

According to Wikipedia, the State pattern is a behavioralsoftware design pattern to allow an object to alter its behavior when its internal state changes. This pattern is close to the concept of Finite-state machine however it is important to keep in mind that this pattern is not a software implementation of Finite-state machine. The state pattern can be interpreted as a strategy pattern which is able to switch the current strategy through invocations of methods defined in the pattern’s interface.

What problems can the State design pattern solve?

An object should change its behavior when its internal state changes.

State-specific behavior should be defined independently. That is, new states should be added and the behavior of existing states should be changed independently.

Implementing state-specific behavior directly within a class is inflexible because it commits the class to a particular behavior and makes it impossible to add a new state or change the behavior of an existing state later independently from (without changing) the class.

What solution does the State design pattern describe?

Define separate (state) objects that encapsulate state-specific behavior for each state. That is, define an interface (State) for performing state-specific behavior, and define classes that implement the interface for each state.

A class delegates state-specific behavior to its current state object instead of implementing state-specific behavior directly.

This makes a class independent of how state-specific behavior is implemented. New states can be added by defining new state classes. A class can change its behavior at run-time by changing its current state object.

UML Diagram

The following diagram shows the State Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

note top of Context The Context is the class that can have a number of internal states. In out example, the GumballMachine is the context. end note

note left of Context::request Whenever the request() is made on the Context it is delegated to the state to handle:

“”state.handle()””

end note

note top of State The State interface defines a common interface for all concrete states; the states all implement the same interface, so they are interchangeable. end note

note “ConcreteStates handle requests from the\nContext. Each ConcreteState provides its\nown implementation for a request. In this\nway, when the Context changes state, its\n behavior will change as well.” as n1

ConcreteStateA .. n1 ConcreteStateB .. n1

note right of ConcreteStateB Many concrete states are possible. end note @enduml

[/plantuml]

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern: