Definition: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
Applicable: When you need to access a clustered object and traverse all these objects, you should consider using the iterator mode. Alternatively, you can use the iterator mode when you need to perform aggregation in multiple ways.
Although we do not need to explicitly reference the iterator, the system still implements traversal through the iterator. In general, the iterator mode separates the traversal behavior of the set object and abstracts an iterator class to take charge of it, so that the internal structure of the set is not exposed, in addition, external code can transparently access the internal data of the set. The iterator mode is widely used when accessing data such as arrays, sets, and lists, especially database data operations.
Structure:
Basic code:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace iterator Mode
{
Class Program
{
Static void main (string [] ARGs)
{
Concreteaggregate A = new concreteaggregate ();
A [0] = "laruence ";
A [1] = "coriander ";
A [2] = "Luggage ";
A [3] = "foreigner ";
A [4] = "Bus internal staff ";
A [5] = "thief ";
Iterator I = new concreteiterator ();
// Iterator I = new concreteiteratordesc ();
Object item = I. First ();
While (! I. isdone ())
{
Console. writeline ("{0} buy a ticket! ", I. currentitem ());
I. Next ();
}
Console. Read ();
}
}
Abstract class Aggregate
{
Public abstract iterator createiterator ();
}
Class concreteaggregate: Aggregate
{
Private ilist <Object> items = new list <Object> ();
Public override iterator createiterator ()
{
Return new concreteiterator (this );
}
Public int count
{
Get {return items. Count ;}
}
Public object this [int Index]
{
Get {return items [Index];}
Set {items. insert (index, value );}
}
}
Abstract class iterator
{
Public abstract object first ();
Public abstract object next ();
Public abstract bool isdone ();
Public abstract object currentitem ();
}
Class concreteiterator: iterator
{
Private concreteaggregate aggregate;
Private int current = 0;
Public concreteiterator (concreteaggregate aggregate)
{
This. Aggregate = aggregate;
}
Public override object first ()
{
Return aggregate [0];
}
Public override object next ()
{
Object ret = NULL;
Current ++;
If (current <aggregate. Count)
{
Ret = aggregate [current];
}
Return ret;
}
Public override object currentitem ()
{
Return aggregate [current];
}
Public override bool isdone ()
{
Return Current> = aggregate. Count? True: false;
}
}
Class concreteiteratordesc: iterator
{
Private concreteaggregate aggregate;
Private int current = 0;
Public concreteiteratordesc (concreteaggregate aggregate)
{
This. Aggregate = aggregate;
Current = aggregate. Count-1;
}
Public override object first ()
{
Return aggregate [aggregate. Count-1];
}
Public override object next ()
{
Object ret = NULL;
Current --;
If (current> = 0)
{
Ret = aggregate [current];
}
Return ret;
}
Public override object currentitem ()
{
Return aggregate [current];
}
Public override bool isdone ()
{
Return Current <0? True: false;
}
}
}