iterator pattern
The Iterator Pattern (Iterator Pattern) is a very commonly used design pattern in Java and .Net programming environments. This pattern is used to access the elements of a collection object sequentially without knowing the underlying representation of the collection object.
The iterator pattern is a behavioral pattern.
Introduction
Intent: Provide a method to sequentially access each element in an aggregate object without exposing the internal representation of the object.
Main solution: Different ways to traverse the entire integration object.
When to use: Traverse an aggregate object.
How to solve: Give the responsibility of walking between elements to the iterator, not the aggregate object.
Key code: Define interface: hasNext, next.
Application example: iterator in JAVA.
Advantages: 1. It supports traversing an aggregate object in different ways. 2. Iterators simplify aggregate classes. 3. There can be multiple traversals on the same aggregate. 4. In the iterator mode, it is very convenient to add new aggregate classes and iterator classes without modifying the original code.
Disadvantages: Since the iterator pattern separates the responsibilities of storing data and traversing data, adding a new aggregate class requires adding a new iterator class, and the number of classes increases in pairs. This increases the complexity of the system to a certain extent.
Usage scenarios: 1. Access the contents of an aggregate object without exposing its internal representation. 2. It is necessary to provide multiple traversal methods for aggregate objects. 3. Provide a unified interface for traversing different aggregate structures.
Note: The iterator pattern separates the traversal behavior of the collection object and abstracts an iterator class to take charge. This way, the internal structure of the collection is not exposed, and the internal structure of the collection is not exposed. Let external code transparently access the data inside the collection.
Implementation
We will create a Iterator interface that describes the navigation method and a Container interface that returns an iterator. Entity classes that implement 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 an interface.
Iterator.java
public interface Iterator { public boolean hasNext(); public Object next(); }
Container.java
public interface Container { public Iterator getIterator(); }
Step 2
Created and implementedContainer Entity class of the 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 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); } } }
Step 4
Verify the output.
Name : Robert Name : John Name : Julie Name : Lora