Overview:
Iterator: provides a method to sequentially aggregate elements of an object without exposing the internal representation of the object.
Practical scenarios:
1. Access the content of an aggregate object without exposing its internal representation.
2. Supports multiple traversal of aggregate objects.
3. provides a unified interface (that is, multi-state iteration) for Traversing different aggregation structures ).
Class diagram:
CodeStructure example:
1. iterator abstract class
/// <Summary>
/// Iterator abstract class
/// Defines the start object, next object, whether to end, current object method, and unified interface.
/// </Summary>
Abstract Class Iterator
{
Public Abstract Object First ();
Public Abstract Object Next ();
Public Abstract Bool Isdone ();
Public Abstract Object Currentitem ();
}
2. Implementation iterator class
Class Concreteiterator: iterator
{
/// <Summary>
/// Defines a specific aggregation object
/// </Summary>
Private Concreteaggregate aggregate;
Private Int Current = 0 ;
/// <Summary>
/// The initialization object passes in a specific clustering class
/// </Summary>
/// <Param name = "aggregate"> </param>
Public Concreteiterator (concreteaggregate aggregate)
{
This . Aggregate = Aggregate;
}
/// <Summary>
/// First object
/// </Summary>
/// <Returns> </returns>
Public Override Object First ()
{
Return Aggregate [ 0 ];
}
/// <Summary>
/// Get the next object of the aggregation
/// </Summary>
/// <Returns> </returns>
Public Override Object Next ()
{
Object RET = Null ;
Current ++ ;
If (Current < Aggregate. Count)
{
RET = Aggregate [current];
}
Return RET;
}
/// <Summary>
/// End?
/// </Summary>
/// <Returns> </returns>
Public Override Bool Isdone ()
{
Return Current > = Aggregate. Count ? True : False ;
}
/// <Summary>
/// Returns the current clustered object.
/// </Summary>
/// <Returns> </returns>
Public Override Object Currentitem ()
{
Return Aggregate [current];
}
}
3. Aggregate abstract classes
/// <Summary>
/// Aggregation abstract class
/// </Summary>
Abstract Class Aggregate
{
/// <Summary>
/// Create iterator
/// </Summary>
/// <Returns> </returns>
Public Abstract Iterator createiterator ();
}
4. Aggregation implementation class
Class Concreteaggregate: Aggregate
{
Private Ilist < Object > Items = New List < Object > ();
Public Override Iterator createiterator ()
{
Return New Concreteiterator ( This );
}
/// <Summary>
/// Returns the total number of aggregates.
/// </Summary>
Public Int Count
{
Get { Return Items. Count ;}
}
/// <Summary>
/// Declare an Indexer
/// </Summary>
/// <Param name = "Index"> </param>
/// <Returns> </returns>
Public Object This [ Int Index]
{
Get { Return Items [Index];}
Set {Items. insert (index, value );}
}
}
5. Client implementation
/// <Summary>
/// Test iterator Mode
/// </Summary>
Static Void Testiterator ()
{
// Clustering object
Concreteaggregate = New Concreteaggregate ();
A [ 0 ] = " Zhang San " ;
A [ 1 ] = " Li Si " ;
A [ 2 ] = " Ye Peng " ;
// Declare iterator object
Iterator I = New Concreteiterator ();
Object Item = I. First ();
While ( ! I. isdone ())
{
Console. writeline ( " {0} go home for dinner " , I. currentitem ());
I. Next ();
}
Console. Read ();
}
Summary:
In fact, the. NET Framework has prepared the iterator interface, just implement the interface.
Ieumerator supports simple iteration of non-generic Sets
The above example can be implemented with a foreach, but it is better to understand the idea of the iterator.