6.19. Iterator mode

发布时间 :2025-10-25 12:24:46 UTC      

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.

6.19.1. Introduction

意图: 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.

6.19.2. Realize

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 .

迭代器模式的 UML 图

6.19.3. Step 1

Create an interface:

Iterator.java

publicinterfaceIterator{publicbooleanhasNext();publicObjectnext();}

Container.java

publicinterfaceContainer{publicIteratorgetIterator();}

6.19.4. Step 2

Create and implement the Container The entity class of the interface. This class has been implemented Iterator Inner class of the interface NameIterator .

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

Use NameRepository To get the iterator and print the name.

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

Execute the program and output the result:

Name : Robert
Name : John
Name : Julie
Name : Lora

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.