Iterator Pattern
# Iterator Pattern
The Iterator Pattern is a very commonly used design pattern in Java and .Net programming environments.
The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
The Iterator Pattern is a behavioral design pattern.
## Introduction
### Intent
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
### Problem Solved
* Provides a uniform method to traverse different aggregate objects.
### Use Cases
* When you need to traverse an aggregate object without exposing its internal structure.
### Implementation Approach
* **Define an Iterator interface**: Contains methods like `hasNext()` and `next()` for traversing elements.
* **Create Concrete Iterators**: Implement 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, containing the traversal logic.
### Application Examples
* **Iterator in Java**: The iterator in the Java Collections Framework is used to traverse collection elements.
### Advantages
1. **Supports Multiple Traversal Methods**: Different iterators can define different traversal methods.
2. **Simplifies Aggregate Classes**: Aggregate classes do not need to care about the traversal logic.
3. **Supports Multiple Traversals**: Multiple traversals can be performed on the same aggregate object simultaneously.
4. **Extensibility**: Adding new aggregate classes and iterator classes is convenient without modifying existing code.
### Disadvantages
* **Increased System Complexity**: For each new aggregate class, a corresponding iterator class needs to be added, increasing the number of classes.
### Usage Recommendations
* Use the Iterator Pattern when you need to access the contents of an aggregate object without exposing its internal representation.
* Consider using the Iterator Pattern when you need to provide multiple traversal methods for an aggregate object.
### 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 its internal structure.
### Structure
The Iterator Pattern includes the following main roles:
* **Iterator Interface**: Defines methods for accessing and traversing the elements of an aggregate object, typically including methods to get the next element, check if there are more elements, and get the current position.
* **Concrete Iterator**: Implements the Iterator interface, responsible for traversing and accessing the aggregate object, while recording the current traversal position.
* **Aggregate Interface**: Defines an interface for creating an iterator object, typically including a factory method for creating iterator objects.
* **Concrete Aggregate**: Implements the aggregate interface, responsible for creating concrete iterator objects and providing the data to be traversed.
## Implementation
We will create an _Iterator_ interface that narrates the navigation method and a _Container_ interface that returns an iterator. The entity class implementing the _Container_ interface will be responsible for implementing the _Iterator_ interface.
_IteratorPatternDemo_, our demo class uses the entity class _NamesRepository_ to print the _Names_ stored as a collection in _NamesRepository_.

### Step 1
Create interfaces:
## Iterator.java
public interface Iterator{public boolean hasNext(); public Object next(); }
## Container.java
public interface Container{public Iterator getIterator(); }
### Step 2
Create entity classes that implement the _Container_ interface. This class has an inner class _NameIterator_ that implements the _Iterator_ interface.
## 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
Execute the program, output the result:
Name : RobertName : JohnName : JulieName : Lora
YouTip