Source code for this article: GitHub. Click here || GitEE. Click here
I. Iterator Mode
1. Basic concepts
Iterator mode, also known as cursor mode, is the behavior mode of objects.Iterator patterns can sequentially access elements in a cluster without exposing the internal representation of the cluster.
2. Pattern Diagram
3. Core Role
- Iterator: Iterator role
This Abstract role defines the interfaces required to traverse elements.
- ConcreteIterator: Specific iterator role
This role implements the Iterator interface and maintains the cursor position during iteration.
- Aggregate: Aggregate Roles
This Abstract role gives the interface for creating Iterator objects.
- ConcreteAggregate: Specific Aggregate Roles
Aggregate holds a collection of objects, providing a way to return an iterator that can be traversed correctly.
- Client: Client role
Holds references to the aggregate and its iterator objects and calls the iterator object's iteration interface.
4. Source Code Cases
public class C02_Iterator { public static void main(String[] args) { Object[] objArray = {"one","two","three","four","five"}; Aggregate aggregate = new ConcreteAggregate(objArray); Iterator iterator = aggregate.createIterator(); while (!iterator.isEnd()){ System.out.println(iterator.currentItem()); iterator.next(); } } } interface Iterator { void first(); void next(); boolean isEnd(); Object currentItem(); } class ConcreteIterator implements Iterator{ //Hold aggregated objects iterated private ConcreteAggregate agg; //Record current iteration index position private int index = 0; //Set the size of the current clustered object private int size = 0; public ConcreteIterator (ConcreteAggregate agg){ this.agg = agg; this.size = agg.getSize(); index = 0; } @Override public void first() { index = 0; } @Override public void next() { if (index<size){ index++; } } @Override public boolean isEnd() { return (index>=size); } @Override public Object currentItem() { return agg.getElement(index); } } abstract class Aggregate { // Create an interface for the corresponding iterator object public abstract Iterator createIterator(); } class ConcreteAggregate extends Aggregate{ private Object[] objArray = null; public ConcreteAggregate (Object[] objArray){ this.objArray = objArray; } @Override public Iterator createIterator() { return new ConcreteIterator(this); } public Object getElement (int index){ if (index<objArray.length){ return objArray[index]; } else { return null; } } public int getSize (){ return objArray.length; } }
2. JDK Collection Application
1. Simple cases
public class C02_ArrayList { public static void main(String[] args) { List<String> stringList = new ArrayList<>() ; stringList.add("One") ; stringList.add("Two") ; stringList.add("Three") ; java.util.Iterator<String> itr = stringList.iterator() ; while (itr.hasNext()){ System.out.println(itr.next()); } } }
2. Iterator Source
Some methods for iterating sets are specified.
public interface Iterator<E> { boolean hasNext(); E next(); default void remove() { throw new UnsupportedOperationException("remove"); } default void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } }
3. ArrayList Source
- Implement Aggregation Interface List
ArrayList<E> extends AbstractList<E> implements List<E>
- Internal Iterator Interface Implementation
private class Itr implements Iterator<E> { int cursor; int lastRet = -1; int expectedModCount = modCount; Itr() {} public boolean hasNext() {} public E next() {} public void remove() {} public void forEachRemaining(Consumer<? super E> consumer) {} final void checkForComodification() {} }
- Return Iterator
public Iterator<E> iterator() { return new Itr(); }
3. Iterator Summary
1. Scenarios applicable
The iterator pattern is set-bound, and whenever a set is used, an iterator for the same set is needed to iterate through the data in the set. Container objects Collection, List, Set, Map in java have their own iterators.Container objects are very core in the programming language, so there are basically matching iterators when implementing containers to meet the needs of development, so there are fewer custom practice scenarios for iterators.
2. Summary of Advantages
Simplify set traversal, where each clustered object can have one or more iterator objects, each iterator's iteration state can be independent of each other.Traversal algorithms are encapsulated in the iterator role, so the iteration algorithm can be independent of the aggregation role changes.
4. Source code address
GitHub·address https://github.com/cicadasmile/model-arithmetic-parent GitEE·address https://gitee.com/cicadasmile/model-arithmetic-parent