Iterator mode analysis, structure diagram, and basic code

Source: Internet
Author: User

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;
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.