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 behavioral software 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“:

PlantUML Syntax:</p><p>@startuml<br /> class Context {<br /> void request()<br /> State state<br /> }<br /> interface State {<br /> {abstract} handle()<br /> }<br /> together {<br /> class ConcreteStateA<br /> class ConcreteStateB<br /> }</p><p>ConcreteStateA : handle()<br /> ConcreteStateB : handle()</p><p>Context -> State<br /> State <|– ConcreteStateA<br /> State <|– ConcreteStateB</p><p>note top of Context<br /> The Context is the class that<br /> can have a number of internal<br /> states. In out example, the<br /> GumballMachine is the context.<br /> end note</p><p>note left of Context::request<br /> Whenever the request()<br /> is made on the Context<br /> it is delegated to the<br /> state to handle:</p><p>“”state.handle()””</p><p>end note</p><p>note top of State<br /> The State interface defines a common<br /> interface for all concrete states; the<br /> states all implement the same interface,<br /> so they are interchangeable.<br /> end note</p><p>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</p><p>ConcreteStateA .. n1<br /> ConcreteStateB .. n1</p><p>note right of ConcreteStateB<br /> Many concrete states<br /> are possible.<br /> end note<br /> @enduml</p><p>

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:

@startuml
class Context {
void request()
State state
}
interface State {
{abstract} handle()
}
together {
class ConcreteStateA
class ConcreteStateB
}

ConcreteStateA : handle()
ConcreteStateB : handle()

Context -> State
State <|-- ConcreteStateA
State <|-- ConcreteStateB

@enduml

Other Design Patterns

In another article you find information about how to put together a single-side web application using PlantUML.

[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]