Iterator Pattern
The Iterator Pattern (Iterator Pattern) is a very commonly used design pattern in Java and .Net programming environments.
The Iterator Pattern provides a way to sequentially access the individual elements of an aggregate object without exposing its internal representation.
The Iterator Pattern belongs to behavioral patterns.
Introduction
Intent
Allows sequential access to elements of an aggregate object without exposing the object's internal representation.
Main problem solved
- Provides a unified method to traverse different aggregate objects.
Use Cases
- When you need to traverse an aggregate object but do not want to expose its internal structure.
Implementation
- Define the iterator interface: Contains
hasNext()andnext()and other methods, used to traverse elements. - Create the concrete iterator: Implements the iterator interface, defining how to traverse a specific aggregate object.
- Aggregate class: Defines an interface for returning an iterator object.
Key code
- Iterator interface: Specifies the methods for traversing elements.
- Concrete iterator: Implements the iterator interface and contains the traversal logic.
Application Example
- Iterator in Java: The iterator in the Java Collections Framework is used to traverse collection elements.
Advantages
- Supports multiple traversal methods: Different iterators can define different traversal methods.
- Simplifies aggregate classes: Aggregate classes do not need to worry about traversal logic.
- Multiple traversal support: Multiple traversals of the same aggregate object can be performed simultaneously.
- Extensibility: Adding new aggregate classes and iterator classes is convenient, with no need to modify existing code.
Disadvantages
- System complexity: Each additional aggregate class requires a corresponding iterator class, increasing the number of classes.
Usage recommendation
- When you need to access the contents of an aggregate object without exposing its internal representation, use the Iterator Pattern.
- When you need to provide multiple traversal methods for an aggregate object, consider using the Iterator Pattern.
Notes
- The Iterator Pattern separates the traversal behavior of collection objects, allowing external code to transparently access the internal data of a collection without exposing the collection's internal structure.
structures
The Iterator Pattern contains the following main roles:
-
Iterator Interface (Iterator): Defines the methods for accessing and traversing the individual elements of an aggregate object, usually including methods to get the next element, determine whether there are more elements, get the current position, etc.
-
Concrete Iterator (Concrete Iterator): Implements the iterator interface, responsible for traversing and accessing the aggregate object, while recording the current position of traversal.
-
Aggregate Object Interface (Aggregate): Defines the interface for creating iterator objects, usually including a factory method for creating the iterator object.
-
Concrete Aggregate Object (Concrete Aggregate): Implements the aggregate object interface, responsible for creating concrete iterator objects and providing the data to be traversed.
Create a class that implements
We will create an interface describing navigation methods Iterator interface and an interface that returns an iterator Container interface. The entity classes that implement the Container interface will be responsible for implementing the Iterator interface.
IteratorPatternDemo, and our demo class uses the entity class NamesRepository to print the NamesRepository that are stored as a collection in the Names。
Step 1
Create the interface:
Iterator.java
public
interface
Iterator
{
public
boolean
hasNext
(
)
;
public
Object
next
(
)
;
}
Container.java
public
interface
Container
{
public
Iterator
getIterator
(
)
;
}
Step 2
Create concrete classes that implement the Container Entity class of the interface. This class has an inner class that implements the Iterator interface NameIterator。
NameRepository.java
public
class
NameRepository
implements
Container
{
public
String
[
]
names
=
{
"
Robert
"
,
"
John
"
,
"
Julie
"
,
"
Lora
"
}
; @
Override
public
Iterator
getIterator
(
)
{
return
new
NameIterator
(
)
;
}
private
class
NameIterator
implements
Iterator
{
int
index
; @
Override
public
boolean
hasNext
(
)
{
if
(
index
<
names
.
length
)
{
return
true
;
}
return
false
;
}
@
Override
public
Object
next
(
)
{
if
(
this
.
hasNext
(
)
)
{
return
names
[
index
++
]
;
}
return
null
;
}
}
}
Step 3
Use NameRepository to get the iterator and print the names.
IteratorPatternDemo.java
public
class
IteratorPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
NameRepository
namesRepository
=
new
NameRepository
(
)
;
for
(
Iterator
iter
=
namesRepository
.
getIterator
(
)
;
iter
.
hasNext
(
)
;
)
{
String
name
=
(
String
)
iter
.
next
(
)
;
System
.
out
.
println
(
"
Name :
"
name
)
;
}
}
}
Step 4
Run the program, output result:
Name : Robert
Name : John
Name : Julie
Name : Lora