iterator pattern dr. neal cis 480. iterator an iterator pattern can be used when one class is a...

13
Iterator Pattern Iterator Pattern Dr. Neal Dr. Neal CIS 480 CIS 480

Post on 21-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Iterator PatternIterator Pattern

Dr. NealDr. Neal

CIS 480CIS 480

Page 2: 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

IteratorIterator

• An iterator pattern can be used when one class is a collection of things and would like to provide a standardized method of accessing it’s collection to another class.

• In Microsoft’s C# the .NET framework defines two interfaces for solving the iterator problem.

Page 3: 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

Microsoft C# InterfacesMicrosoft 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

Page 4: 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

Interface Class DiagramInterface Class Diagram

IEnumerable

GetEnumerator() : IEnumerator

<<Interface>>IEnumerator

Current : object

MoveNext() : boolReset() : void

<<Interface>>

Page 5: 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

Iterator Pattern ExampleIterator 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.

Page 6: 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

Example User InterfaceExample User Interface

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

Page 7: 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

Classes Classes for our for our

ExampleExampleCart

cart : string[]length : int

Cart()GetEnumerator() : IEnumerator

EnumCart

count : intlist : object[]Current : object

EnumCart(c : object[])MoveNext() : boolReset() : void

Form1

btnDumpCart : ButtonlblCartEnumeration : LabellblResults : Label

Form1()dumpCartClick(o : object, e : Eventargs) : voidDispose(b : bool) : voidInitializeComponent() : void

IEnumerable

GetEnumerator() : IEnumerator

(from .NET)

<<Interface>>IEnumerator

Current : object

MoveNext() : boolReset() : void

(from .NET)

<<Interface>>

instantiates

instantiatesuses

Page 8: 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

Cart Cart ClassClass

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

// this method returns a object which implements the// the IEnumerator interface so that it can be used// in an enumerationpublic IEnumerator GetEnumerator(){

return new EnumCart(cart);}

#endregion}

Implemention of

IEnumerable Interface

Page 9: 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

EnumCart EnumCart ClassClass

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

Implemention of IEnumerator

Interface

Page 10: 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

Event Procedure in FormEvent Procedure in Form

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

Cart c = new Cart();foreach(string s in c){

lblResults.Text = lblResults.Text + s + "\n";}

}

Use foreach to

iterate through the Cart

Page 11: 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

The Magic of Objects and The Magic of Objects and CompilersCompilers

• Note that in our implementation no code never references the class “EnumCart” or calls the GetEnumerator() method in “Cart”

• Let’s look behind the scenes and determine what the compiler is doing to our code.

Page 12: 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

A A foreachforeach Enumeration in C# Enumeration in C#

Cart c = new Cart();foreach (string s in c){

lblResults.Text = lblResults.Text + s + "\n";}

Cart c = new Cart();IEnumerator e = c.GetEnumerator();while (e.MoveNext()){

string s = (string) e.Current;lblResults.Text = lblResults.Text + s + "\n";

}

ProgrammerCreated

Code

C# CompilerGenerated

Code

Page 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

ResultingResultingSequence Sequence DiagramDiagram

: User : User : Form1 : Form1 : Cart : Cart : EnumCart : EnumCart

2: Cart( )

1: dumpCartClick(object, Eventargs)

3: GetEnumerator( )

4: MoveNext( )

5: Current

Complier Generated Code