The structure of the iterator pattern:
The iterative sub-pattern can access an aggregated element sequentially without exposing the aggregated internal representation.
Iteratives can be divided into external iteratives and internal iteratives .
External iterator: suitable for white box aggregation (white box aggregation is to provide the outside world with an aggregation that accesses its internal element interface). Since the iterative logic is provided by the aggregation object itself, such external iterator sub-role often only maintains the cursor position of the iterative. Therefore, the specific iterative sub-role is an external class, and its constructor accepts a specific aggregate object, so that it can call the iterative logic of the aggregate object.
Inner iterator: suitable for black box aggregation (black box aggregation does not provide an interface to the outside to traverse its own element objects). Since element objects gathered by black box can only be accessed by the aggregated internal members, the inner iterator can only be a subclass of the member inside the aggregate.
Simple demonstration:
package test.edu.inter; public interface IteratorObj { /** * Move to the first element*/ public void first(); /** * Move to the next element*/ public boolean hasNextItem(); /** * Return the current element*/ public Object currentItem(); } package test.edu.inter; public interface DataSet { public IteratorObj getIterator(); } package test.edu.inter; public class Iterator1 implements IteratorObj { private Dataobj set; private int size; private int index=0; public Iterator1(Dataobj set){ this.set = set; this.size = set.getSize(); } @Override public void first() { // TODO Auto-generated method stub this.index = 0; } @Override public boolean hasNextItem() { if(index<size){ return true; } return false; } @Override public Object currentItem() { Object ob = set.getItem(index); if(index<size){ index++; } return ob; } } package test.edu.inter; public class Dataobj implements DataSet { private Object[] objArray = null; /** * Incoming aggregate object*/ public Dataobj(Object[] objArray){ this.objArray = objArray; } @Override public IteratorObj getIterator() { return new Iterator1(this); } public Object getItem(int index){ return objArray[index]; } public int getSize(){ return objArray.length; } } package test.edu.inter; public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String[] str={"12312","dasda","dasd","12d","asd"}; Dataobj ao = new Dataobj(str); IteratorObj io = ao.getIterator(); while(io.hasNextItem()){ System.out.println(io.currentItem()); } } } Running results:
12312 dasda dasd 12d asd
Content expansion: Applications in Java aggregation
The iterator() factory method is provided in the java.util.Collection interface to return an Iterator type object. The subtype of the Collection interface, the inner member class Itr implements the Iterator interface. So Itr is an intrinsic iterative subclass, but AbstractList also provides its own traversal method, so it is not a black box aggregation, but a white box aggregation. The code is as follows:
import java.util.Iterator;public interface Itr extends Iterator{ //Indicator used when calling the next() method again int cursor = 0; //Indicator used in the last call lastRet = -1; int expectedModCount = modCount; public boolean hasNext(){ return cursor!=size(); } public Object next(){ try{ Object next = get(cursor); checkForComodification(); lastRet = cursor++; return next; }catch(IndexOutOfBoundsException e){ checkForComodification(); throw new NoSuchElementException(); } } //Delete the last traversed element, the remove() method can only delete the last traversed element public void remove(){ if(lastRet ==-1) throw new IllegalStateException(); checkForComodification(); try{ AbstractList.this.remove(lastRet); if(lastRet<cursor) cursor--; lastRet = -1; expectedModCount = modCount; }catch(IndexOutOfBoundsException e){ throw new ConcurrentModificationException(); } } public void checkForComodification(){ if(modCount!=expectedModCount) throw new ConcurrentModificationException(); }} The variables and methods such as modCount, get(cursor) are all owned by the AbstractList class, and Itr can be used directly. The method checkForComodification() will check whether the aggregated content has just been directly modified by the outside world (not modified through the remove() method provided by the iterator). If the gathered content is bypassed by the iterative sub-object and directly modified the New Year after the iterative sub-object, this method will immediately throw an exception.
In addition: The AbstractList class also provides the listIterator() method, returning a class ListItr instance that implements the Listiterator interface. The ListIterator interface implements forward iteration and reverse iteration, and also provides a method to safely modify the content of the column during the iteration process.
The difference between Enumeration and Iterator: (1) Enumeration does not have a remove method (2) Enumeration is implemented as an unnamed class in the element() method in Vector. It does not pay for Fail Fast, that is, during the iteration process, the aggregated object is unexpectedly modified by the outside world, and this iteration process will immediately catch any exceptions.
The above is all about this article, I hope it will be helpful to everyone's learning.