Design Patterns: StateBlog

Software Design Patterns: State

post-thumb

Introduction

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”:

State Pattern

You can click on the diagram to open it online in Pladitor.

Diagram Sources

Here are the diagram sources:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@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

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

Open this diagram online with Pladitor.

Share: