ArticleDirectory
Dr. Yan Hong's book "Java and patterns" describes the iterator mode in this way:
The iteration submode is also called the cursor mode, which is the behavior mode of objects. The iteration submode can sequentially access an element in the aggregation without exposing the internal representation of the aggregation (Internal Representation).
Aggregation and Java Aggregation
The aggregation of multiple objects is generally called aggregate. A clustering object is a container object that can accommodate a group of objects. Aggregation depends on the abstraction of the aggregation structure, which is complex and diverse. Arrays are the most basic aggregation and are also the basis for designing other Java clustering objects.
Java clustering object is an object that implements the common Java. util. Collection interface and is directly supported by Java. Since version 1.2, Java provides many clustering types, including vector, arraylist, hashset, hashmap, and hashtable. These are examples of Java aggregation.
Structure of the iteration submode
There are two implementation methods for the iteration submode:White Box aggregation and intrinsic IterationAndBlack Boxes are clustered in inner iterations.
White Box aggregation and intrinsic Iteration
If a clustering interface provides a method that can be used to modify clustering elements, this interface is calledWide Interface.
If the clustering object provides the same interface, that is, the wide interface for all objects, it will certainly meet the requirements of the iteration sub-Object Mode for iteration sub-objects. However, this will destroy the encapsulation of the clustered object. This aggregation that provides wide interfaces is calledWhite Box Aggregation. The aggregation object provides the same wide interface to the outside world, as shown in:
The aggregation implements the iteration logic by itself and provides an appropriate interface to the outside, so that the iteration sub can control the iteration process of the clustering element from the outside. In this way, the iterator only controls a cursor, which is calledCursor iterator). Since iterations are outside the clustering structure, such iterations are also calledExtrinsic iterator).
Now let's take a look at the implementation of the white box clustering and the intrinsic iteration. A white box aggregation interface (called the Traversal method or the traversing method) provides external users with an interface to access their internal elements, so that the external iterator can implement the iteration function through the aggregation Traversal method.
Because the iteration logic is provided by the clustering object itself, such an extra iteration subrole usually only maintains the cursor position of the iteration.
Shows a typical system composed of white box aggregation and intrinsic iterations. In this implementation, the specific iteration subrole is an external class, the specific clustering role provides external interfaces for Traversing clustering elements.
The iteration submode involves the following roles:
● Abstract iteration sub-(iterator) Role: This abstract role defines the interface required to traverse elements.
● Concreteiterator: this role implements the iterator interface and maintains the cursor position during the iteration.
● Aggregate role: This abstract role provides an interface for creating an iterator object.
● Concreteaggregate role: implements the interface for creating iteration subobjects and returns a suitable iteration subinstance.
● Client role: holds a reference to clustering and Its Iterative sub-objects, calls the iterative interface of iterative sub-objects, and may also add or delete clustering elements through iterative sub-operations.
Source code
Abstract aggregation class, which specifies all the interfaces required for specific aggregation. The iteration sub-mode requires that the clustering object must have a factory method, that is, the createiterator () method, to provide external instances of iterative sub-objects.
Public Abstract ClassAggregate {/*** The Factory method creates an interface for the corresponding iteration sub-Object*/Public AbstractIterator createiterator ();}
The aggregation class implements the interface required by the abstract aggregation class, that is, the createiterator () method. In addition, the getelement () method also provides clustering elements to the outside world, while the size () method provides clustering size to the outside world.
Public Class Concreteaggregate Extends Aggregate { Private Object [] objarray = Null ; /** * Constructor to pass in the specific content of the aggregate object */ Public Concreteaggregate (object [] objarray ){ This . Objarray = Objarray;} @ override Public Iterator createiterator (){ Return New Concreteiterator ( This );} /** * Value method: Provide clustering elements to the outside world. */ Public Object getelement ( Int Index ){ If (Index < Objarray. Length ){ Return Objarray [Index];} Else { Return Null ;}} /** * Value method: Provide the size of the aggregation to the outside world. */ Public Int Size (){ Return Objarray. Length ;}}
Abstract iteration sub-Role
Public Interface Iterator { /** * Iteration method: Move to the first element. */ Public Void First (); /** * Iteration method: Move to the next element */ Public Void Next (); /** * Iteration method: whether it is the last element */ Public Boolean Isdone (); /** * Iteration method: return the current element */ Public Object currentitem ();}
Specific iteration sub-Role
Public Class Concreteiterator Implements Iterator { // Hold specific aggregate objects to be iterated Private Concreteaggregate aggregate; // Internal index, which records the index location from the current iteration Private Int Index = 0 ; // Record the size of the current clustered object Private Int Size = 0 ; Public Concreteiterator (concreteaggregate iterator ){ This . Bandwidth = Break; This . Size = Rows. Size (); index = 0 ;} /** * Iteration method: return the current element */ @ Override Public Object currentitem (){ Return Response. getelement (INDEX );} /** * Iteration method: Move to the first element. */ @ Override Public Void First () {Index = 0 ;} /** * Iteration method: whether it is the last element */ @ Override Public Boolean Isdone (){ Return (Index> = Size );} /** * Iteration method: Move to the next element */ @ Override Public Void Next (){ If (Index < Size) {Index ++ ;}}}
Client type
Public Class Client { Public Void Operation () {object [] objarray = {"One", "two", "three", "four", "five", "Six" }; // Create an aggregate object Aggregate finished = New Concreteaggregate (objarray ); // Cyclically output aggregate object values Iterator it = Vertex. createiterator (); While (! It. isdone () {system. Out. println (it. currentitem (); it. Next ();}} Public Static Void Main (string [] ARGs) {Client client Client = New Client (); client. Operation ();}}
In the preceding example, a clustering class instance is created, and the createiterator () method of the clustering object is called to obtain an iterative sub-object. After an iteration sub-instance is obtained, the client starts the iteration process and prints all the clustering elements.
Meanings of intrinsic iterations
A frequently asked question is: Since the white box clustering has provided the Traversal method to the outside world, the client can iterate on its own. Why should we apply the iteration submode, and create an iteration sub-object for iteration?
Of course, the client can iterate on its own, and it does not have to need an iterative sub-object. However, the iteration sub-objects and the iteration mode abstract the iteration process and separate the iteration sub-responsibilities of the client as the iteration consumer from the iteration owner so that the two can evolve independently. When the types of clustering objects change, or the iterative methods change, the iteration sub-layer, as an intermediary layer, can absorb the changing factors to avoid modifying the client or clustering itself.
In addition, if the system needs to iterate on several different clustering objects at the same time, and these clustering objects provide different traversal methods, it makes sense to use the iteration submode and an external iteration sub-object. Different iteration sub-objects with the same iteration interface process clustering objects with different traversal interfaces, so that the system can use a unified iteration interface for all iterations.
Black box aggregation and inner iteration
If a clustering interface does not provide a method to modify clustering elements, such an interface is calledNarrow Interface.
Clustering objects provide a wide interface for iterative sub-objects and a narrow interface for other objects. In other words, the internal structure of the clustering object should be made public to the iterative sub-objects so that the iterative sub-objects can have a sufficient understanding of the clustering object and can be iterated. However, clustering objects should avoid providing these methods to other objects, because other objects should perform these operations through iterative sub-objects rather than directly manipulating the clustering objects.