Home » Blog » Software Development » Software Design Patterns in PlantUML » Design Patterns with PlantUML: 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 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 Iterator Pattern?

According to Wikipedia, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container’s elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

What problems can the Iterator design pattern solve?

  • The elements of an aggregate object should be accessed and traversed without exposing its representation (data structures).
  • New traversal operations should be defined for an aggregate object without changing its interface.

Defining access and traversal operations in the aggregate interface is inflexible because it commits the aggregate to particular access and traversal operations and makes it impossible to add new operations later without having to change the aggregate interface.

What solution does the Iterator design pattern describe?

  • Define a separate (iterator) object that encapsulates accessing and traversing an aggregate object.
  • Clients use an iterator to access and traverse an aggregate without knowing its representation (data structures).

Different iterators can be used to access and traverse an aggregate in different ways. New access and traversal operations can be defined independently by defining new iterators.

UML Diagram

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

PlantUML Syntax:
<p>@startuml<br />
class Aggregate <<interface>><br />
class Client<br />
class Iterator <<interface>><br />
together {<br />
class ConcreteAggregate<br />
class ConcreteIterator<br />
}</p>
<p>Aggregate : createIterator()<br />
ConcreteAggregate : createIterator()<br />
Iterator : hasNext()<br />
Iterator : next()<br />
Iterator : remove()<br />
ConcreteIterator : hasNext()<br />
ConcreteIterator : next()<br />
ConcreteIterator : remove()</p>
<p>Aggregate <- Client<br />
Client -> Iterator<br />
ConcreteAggregate -> ConcreteIterator<br />
Iterator <|.. ConcreteIterator<br />
Aggregate <|.. ConcreteAggregate</p>
<p>note top of Aggregate<br />
Having a common interface for your<br />
aggregates is handy for your client;<br />
it decouples your client from the<br />
implementation of your collection of objects.<br />
end note</p>
<p>note bottom of ConcreteAggregate<br />
The ConcreteAggregate<br />
has a collection of<br />
objects and implements<br />
the method that<br />
returns an Iterator<br />
for its collection.<br />
end note</p>
<p>note “Each ConcreteAggregate\nis responsible for\ninstantiating a\nConcreteIterator that\ncan iterate over its\ncollection of objects.” as n1<br />
ConcreteAggregate .. n1<br />
n1 .. ConcreteIterator</p>
<p>note bottom of ConcreteIterator<br />
The ConcreteIterator is<br />
responsible for managing<br />
the current position of<br />
the iteration.<br />
end note</p>
<p>note top of Iterator<br />
The Iterator interface<br />
provides the interface<br />
that all iterators<br />
must implement, and<br />
a set of methods<br />
for traversing over<br />
elements of a collection.<br />
Here we are using the<br />
java.util.Iterator. If you<br />
do not want to use Java’s<br />
Iterator interface, you<br />
can always create your<br />
own.<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 Aggregate <<interface>>
class Client
class Iterator <<interface>>
together {
class ConcreteAggregate
class ConcreteIterator
}

Aggregate : createIterator()
ConcreteAggregate : createIterator()
Iterator : hasNext()
Iterator : next()
Iterator : remove()
ConcreteIterator : hasNext()
ConcreteIterator : next()
ConcreteIterator : remove()

Aggregate <- Client Client -> Iterator
ConcreteAggregate -> ConcreteIterator
Iterator <|.. ConcreteIterator
Aggregate <|.. ConcreteAggregate

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