May 27, 2021 Design mode
Iterator Pattern is a design pattern that is very commonly used in Java and .Net programming environments. This pattern is used to sequentially access the elements of a collection object without knowing the underlying representing of the collection object.
The iterator pattern is a behavioral pattern.
Intent: Provides a way to access individual elements in an aggregate object sequentially without exposing the object's internal representing.
Key solution: Different ways to traverse the entire consolidated object.
When to use: Traverse an aggregate object.
How to solve it: Leave the responsibility for walking between elements to the iterator, not to aggregate objects.
Key code: Define interface: hasNext, next.
Example of the app: Iterator in JAVA.
Pros: 1, it supports traversing an aggregate object in different ways. 2 , iterator simplifies the aggregation class. 3 , on the same aggregation can have more than one traversal. 4 , in the iterator mode, the addition of new aggregate classes and iterator classes are very convenient, do not need to modify the original code.
Cons: Because the iterator pattern separates the responsibility for storing and traversing data, adding new aggregate classes requires adding new iterator classes correspondingly, and the number of classes increases in pairs, which in part increases the complexity of the system.
Use scenarios: 1, access the contents of an aggregated object without exposing its internal representing. 2 , need to provide a variety of traversal methods for aggregate objects. 3 , for traversing different aggregation structures to provide a unified interface.
Note: The iterator pattern separates the traversal behavior of the collection object and abstracts out an iterator class to be responsible, so that the internal structure of the collection is not exposed and external code can transparently access the data inside the collection.
We'll create an Iterator interface that describes the navigation method and a Container interface that returns the iterator. The entity class that implements the Container interface will be responsible for implementing the Iterator interface.
Iterator Pattern Demo, our demo class uses the entity class NamesRepository to print Names stored as a collection in NamesRepository.
Create an interface.
Iterator.java
public interface Iterator { public boolean hasNext(); public Object next(); }
Container.java
public interface Container { public Iterator getIterator(); }
Create an entity class that implements the Container interface. This class has an internal 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; } } }
Use NameRepository to get the iterator and print the name.
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); } } }
Verify the output.
Name : Robert Name : John Name : Julie Name : Lora