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 elements of an aggregate object without exposing its internal representation.
The Iterator pattern is a behavioral pattern.
Introduction
intent
Allows sequential access to elements in an aggregate object while not exposing the object's internal representation.
Main problems solved
- Provides a unified method for traversing different aggregate objects.
Use Case
- When you need to traverse an aggregate object without exposing its internal structure.
Implementation Approach
- Define the iterator interface: contains
hasNext()andnext()and other methods, used to traverse elements. - Create a concrete iterator: Implements the iterator interface and defines 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: Iterators in the Java Collections Framework are used to traverse collection elements.
Advantages
- Supports multiple traversal methods.: Different iterators can define different traversal methods.
- Simplify Aggregate Class: The aggregate class does not need to care about the traversal logic.
- Multiple Traversal Support: Multiple traversals can be performed on the same aggregate object simultaneously.
- Extensibility: Adding new aggregate classes and iterator classes is very convenient without modifying existing code.
Disadvantages
- System Complexity: Each time an aggregate class is added, a corresponding iterator class needs to be added, increasing the number of classes.
Usage suggestions
- 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 collection's internal data without exposing its internal structure.
Structure
The Iterator Pattern includes the following main roles:
Iterator Interface (Iterator): Defines methods for accessing and traversing each element in an aggregate object, typically including getting the next element, checking whether there are more elements, getting the current position, and other methods.
Concrete Iterator (Concrete Iterator): Implements the iterator interface and is responsible for traversing and accessing the aggregate object while recording the current traversal position.
Aggregate interface (Aggregate): Defines an interface for creating iterator objects, typically including a factory method for creating iterator objects.
Concrete Aggregate (Concrete Aggregate): Implements the aggregate interface and is responsible for creating concrete iterator objects and providing the data to be traversed.
Implementation
We will create an interface that describes navigation methods.Iteratorinterface and an interface that returns an iterator.ContainerInterface. ImplementedContainerConcrete classes of the interface will be responsible for implementing.IteratorInterface.
IteratorPatternDemo, our demonstration class uses the concrete class.NamesRepositoryTo printNamesRepositoryStored as a collection inNames。
Step 1
Create Interface:
Iterator.java
Container.java
Step 2
Create an ImplementationContainerconcrete class of the interface. This class has implemented.IteratorInner class of the interfaceNameIterator。
NameRepository.java
Step 3
UsageNameRepositoryto obtain the iterator, and print names.
IteratorPatternDemo.java
Step 4
Run the program, output the results:
Name : Robert Name : John Name : Julie Name : Loraother extensions