Home » Blog » Software Development » Software Design Patterns in PlantUML » Design Patterns with PlantUML: State Pattern

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 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>@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>

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.

Proxy Pattern Design Pattern with PlantUML: Proxy Pattern - This post gives a brief overview about the Proxy Pattern. The post is part of a series about software design ... Read More
State Pattern 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 ... Read More
Composite Pattern Design Patterns with PlantUML: Composite Pattern - This post gives a brief overview about the Composite Pattern. The post is part of a series about software design ... Read More
Iterator Pattern Design Patterns with PlantUML: Iterator Pattern - This post gives a brief overview about the Iterator Pattern. The post is part of a series about software design ... Read More
Template Pattern Design Patterns with PlantUML: Template Pattern - This post gives a brief overview about the Template Pattern. The post is part of a series about software design ... Read More
Facade Pattern Design Patterns with PlantUML: Facade Pattern - This post gives a brief overview about the Facade Pattern. The post is part of a series about software design ... Read More
Adapter Pattern Design Patterns with PlantUML: Adapter Pattern - This post gives a brief overview about the Adapter Pattern. The post is part of a series about software design ... Read More
Command Pattern Design Patterns with PlantUML: Command Pattern - This post gives a brief overview about the Command Pattern. The post is part of a series about software design ... Read More
Singleton Design Patterns with PlantUML: Singleton - This post gives a brief overview about the Singleton Pattern. The post is part of a series about software design ... Read More
Design Patterns with PlantUML: Abstract Factory Pattern - This post gives a brief overview about the Abstract Factory Pattern. The post is part of a series about software design ... Read More

Check Also

Proxy Pattern

Design Pattern with PlantUML: Proxy Pattern

This post gives a brief overview about the Proxy Pattern. The post is part of …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

We use Cookies and similar technology to collect and analyse information about the users of this website. We use this information to enhance the content, advertising and other services available on the site. Please click ‘Accept cookies’ to consent to the use of this technology by petrockblock. You can manage your preferences at any time by visiting our Cookies Policy page.