Provides a way to access an aggregate object without exposing its internal representation.
The UML class diagram is as follows:
The relationship between classes and objects is:
1. iterator: The iterator defines interfaces for accessing and traversing elements.
2. concreteiterator (Specific iterator): implements the iterator interface and tracks the position of the current aggregate object in the time duration.
3. Aggregate (aggregation): defines an interface for creating iterator objects.
4. concreteiterator (specific aggregation): creates an iteration object and returns an instance of a specific iterator.
The following figure shows the order of typical applications:
- Instance 1 -- traversal example:
The following traversal example delays the application of the iterator mode. The example uses iterations to traverse sub-projects in the Set, defines the number of iterations, and skips the sub-projects with specific numbers. The UML class diagram is as follows:
View code
// Project
Class Item
{
String Name;
Public Item ( String Name)
{
This . Name = Name;
}
Public String Name
{
Get { Return Name ;}
}
}
// Aggregate abstract aggregation class
Abstract Class Abstractcollection
{
Abstract Public Iterator createiterator ();
}
// Concreteaggregate specific aggregation
Class Collection: abstractcollection
{
Private Arraylist items = New Arraylist ();
Public Override Iterator createiterator ()
{
Return New Iterator ( This );
}
Public Int Count
{
Get {Return Items. Count ;}
}
// Index
Public Object This [ Int Index]
{
Get { Return Items [Index];}
Set {Items. Add (value );}
}
}
// Abstract iterator
Abstract Class Abstractiterator
{
Abstract Public Item first ();
Abstract Public Item next ();
Abstract Public Bool Isdone ();
Abstract Public Item currentitem ();
}
// Specific iterator
Class Iterator: abstractiterator
{
Private Collection collection;
Private Int Current = 0 ;
Private Int Step =1 ;
Public Iterator (Collection collection)
{
This . Collection = collection;
}
// Step Size
Public Int Step
{
Get { Return Step ;}
Set {Step = value ;}
}
// Initial Project
Public Override Item first ()
{
Current = 0 ;
Return (Item) Collection [current];
}
// Next
Public Override Item next ()
{
Current + = step;
If (! Isdone ())
Return (Item) Collection [current];
Return Null ;
}
// Current Project
Public Override Item currentitem ()
{
Return (Item) Collection [current];
}
// At the end?
Public Override Bool Isdone ()
{
Return Current> = collection. Count? True : False ;
}
}
Class Program
{
Static Void Main ( String [] ARGs)
{
Collection collection = New Collection ();
Collection [ 0 ] = New Item ( " Item 0 " );
Collection [1 ] = New Item ( " Item 1 " );
Collection [ 2 ] = New Item ( " Item 2 " );
Collection [ 3 ] = New Item ( " Item 3 " );
Collection [ 4 ] = New Item ( " Item 4 " );
Collection [ 5 ] = New Item ( " Item 5 " );
Collection [ 6 ] = New Item ( " Item 6 " );
Collection [ 7 ] = New Item ( " Item 7 " );
// Generate iterator
Iterator = New Iterator (Collection );
// Step 2: Skip a project
Iterator. Step = 2 ;
For (Item = iterator. First ();! Iterator. isdone (); item = iterator. Next ())
{
Console. writeline (item. Name );
}
Console. Read ();
}
}
- Advantages and disadvantages:
The iterator mode supports moving cursors in aggregation, which simplifies access to elements in aggregation, simplifies aggregation interfaces, and encapsulates aggregated objects.
The iterator mode can also be used to access the tree structure,ProgramNo need to start from scratchCodeSearching the corresponding location can be controlled to start searching from the subset, which plays an important role in accelerating the program running speed.
The disadvantage of the iterator mode is that aggregation is closely related and coupling is added. However, defining this coupling in the abstract base class can solve this problem.
The following scenarios are suitable for the application iterator mode:
1. You need to traverse the objects in the aggregation but not expose the internal structure of the aggregation.
2. Allow multi-level traversal of aggregation without affecting each other.
3. Provide a consistent interface to traverse different structures in the aggregation.