The iterator pattern (Iterator Pattern) is a very common design pattern in Java and .net programming environments. This pattern is used to sequentially access the elements of the collection object without knowing the underlying representation of the collection object.
The iterator pattern belongs to the behavioral pattern. 意图: Provides a method to sequentially access elements in an aggregated object without exposing the internal representation of the object. 主要解决: Traverse the entire integrated object in different ways. 何时使用: Traverse an aggregate object. 如何解决: Leave the responsibility for wandering between elements to the iterator, not the aggregate object. 关键代码: Define interfaces: hasNext, next. 应用实例: Iterator in JAVA. 优点: 1. It supports traversing an aggregate object in different ways. 2. The iterator simplifies the aggregation class. 3. There can be multiple traverses on the same aggregation. 4. In iterator mode, it is convenient to add new aggregate classes and iterator classes, and there is no need to modify the original code. 缺点: Because the iterator pattern separates the responsibility of storing data from traversing data, adding new aggregation classes requires the corresponding addition of new iterator classes, and the number of classes increases in pairs, which increases the complexity of the system to a certain extent. 使用场景: 1. Access the contents of an aggregate object without exposing its internal representation. 2. Multiple traversal methods need to be provided for aggregate objects. 3. Provide a unified interface for traversing different aggregation structures. 注意事项: The iterator pattern separates the traversal behavior of the collection object and abstracts an iterator class to be responsible for it, which not only does not expose the internal structure of the collection, but also allows external code to access the data inside the collection transparently. We will create a description of the navigation method Iterator Interface and an object that returns the iterator Container Interface. Realized Container The entity class of the interface is responsible for implementing the Iterator Interface. IteratorPatternDemo Our demo class uses entity classes NamesRepository To print. NamesRepository Stored as a collection in the Names . Create an interface: Create and implement the Container The entity class of the interface. This class has been implemented Iterator Inner class of the interface NameIterator . Use NameRepository To get the iterator and print the name. Execute the program and output the result: 6.19.1. Introduction ¶
6.19.2. Realize ¶

6.19.3. Step 1 ¶
Iterator.java ¶
publicinterfaceIterator{publicbooleanhasNext();publicObjectnext();}
Container.java ¶
publicinterfaceContainer{publicIteratorgetIterator();}
6.19.4. Step 2 ¶
NameRepository.java ¶
publicclassNameRepositoryimplementsContainer{publicString[]names={"Robert","John","Julie","Lora"};
@OverridepublicIteratorgetIterator(){returnnewNameIterator();}privateclassNameIteratorimplementsIterator{intindex;
@OverridepublicbooleanhasNext(){if(index<names.length){returntrue;}returnfalse;}@OverridepublicObjectnext(){if(this.hasNext()){returnnames[index++];}returnnull;}}}
6.19.5. Step 3 ¶
IteratorPatternDemo.java ¶
publicclassIteratorPatternDemo{publicstaticvoidmain(String[]args){NameRepositorynamesRepository=newNameRepository();for(Iteratoriter=namesRepository.getIterator();iter.hasNext();){Stringname=(String)iter.next();System.out.println("Name
:"+name);}}}
6.19.6. Step 4 ¶
Name : Robert
Name : John
Name : Julie
Name : Lora