0% found this document useful (0 votes)
26 views

It Erator Pattern

ererer

Uploaded by

Said Gahi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

It Erator Pattern

ererer

Uploaded by

Said Gahi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Iterator Pattern

Dr. Neal
CIS 480

Iterator
An iterator pattern can be used when one
class is a collection of things and would
like to provide a standardized method of
accessing its collection to another class.
In Microsofts C# the .NET framework
defines two interfaces for solving the
iterator problem.

Microsoft C# Interfaces
IEnumerable
Defines a method GetEnumerator() which
return a class of type IEnumerator

IEnumerator
Defines methods for MoveNext() and ReSet()
which allow sequential movement through the
collection or reset to the beginning
Defines an attribute/property Current that
contains the current object in the collection

Interface Class Diagram


<<Interface>>
IEnumerable

<<Interface>>
IEnumerator
Current : object

GetEnumerator() : IEnumerator
MoveNext() : bool
Reset() : void

Iterator Pattern Example


Say we have a Shopping Cart of some items,
call this class Cart. This class must implement
the IEnumerable interface.
We will create a class called EnumCart which
is the iterator for Cart. This class must
implement the IEnumerator interface.
Finally, we will create a Form to display our
results using a C# foreach statement to test our
iterator.

Example User Interface


Our user interface
will just display the
items in Cart when
the DumpCart
button is pushed.

<<Interface>>
IEnumerator

<<Interface>>
IEnumerable

(from .NET )

Current : object

Classes
for our
Example

(from .NET )

MoveNext() : bool
Reset() : void

GetEnumerator() : IEnumerator

EnumCart
count : int
list : object[]
Current : object

instantiates

EnumCart(c : object[])
MoveNext() : bool
Reset() : void

uses

Cart
cart : string[]
length : int
Cart()
GetEnumerator() : IEnumerator

instantiates

Form1
btnDumpCart : Button
lblCartEnumeration : Label
lblResults : Label
Form1()
dumpCartClick(o : object, e : Eventargs) : void
Dispose(b : bool) : void
InitializeComponent() : void

Cart
Class

public class Cart : IEnumerable


{
string[] cart;
int length;
public Cart()
{
length = 4;
cart = new string[length];
cart[0] = "Item one";
cart[1] = "Item two";
cart[2] = "Item three";
cart[3] = "Item four";
}
#region IEnumerable Members

Implemention
of
IEnumerable
Interface

// this method returns a object which implements the


// the IEnumerator interface so that it can be used
// in an enumeration
public IEnumerator GetEnumerator()
{
return new EnumCart(cart);
}
#endregion
}

EnumCart
Class

Implemention
of IEnumerator
Interface

public class EnumCart : IEnumerator


{
object[] list;
int count;
public EnumCart(object[] ol)
{
list = ol;
count = -1;
}
#region IEnumerator Members
public void Reset()
{
count = -1;
}
public object Current
{
get
{
return list[count];
}
}
public bool MoveNext()
{
count++;
if (count < list.Length)
return true;
else
return false;
}

Event Procedure in Form

private void dumpCartClick(object sender, System.EventArgs e)


{
Use
Cart c = new Cart();
foreach to
foreach(string s in c)
iterate
{
through
lblResults.Text = lblResults.Text + s + "\n";
the Cart
}
}

The Magic of Objects and


Compilers
Note that in our implementation no code
never references the class EnumCart or
calls the GetEnumerator() method in
Cart
Lets look behind the scenes and
determine what the compiler is doing to
our code.

A foreach Enumeration in C#
Programmer
Created
Code

Cart c = new Cart();


foreach (string s in c)
{
lblResults.Text = lblResults.Text + s + "\n";
}

C# Compiler
Generated
Code

Cart c = new Cart();


IEnumerator e = c.GetEnumerator();
while (e.MoveNext())
{
string s = (string) e.Current;
lblResults.Text = lblResults.Text + s + "\n";
}

Resulting
Sequence
Diagram

: Form1

: User

: Cart

1: dumpCartClick(object, Eventargs)

2: Cart( )

Complier
Generated
Code

3: GetEnumerator( )

4: MoveNext( )
5: Current

: EnumCart

You might also like