1 / 13

Iterator Pattern

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 it’s collection to another class.

Télécharger la présentation

Iterator Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterator Pattern Dr. Neal CIS 480

  2. 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 it’s collection to another class. • In Microsoft’s C# the .NET framework defines two interfaces for solving the iterator problem.

  3. 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

  4. Interface Class Diagram

  5. 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.

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

  7. Classes for our Example

  8. 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 enumeration public IEnumerator GetEnumerator() { return new EnumCart(cart); } #endregion } Cart Class Implemention of IEnumerable Interface

  9. 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; } EnumCart Class Implemention of IEnumerator Interface

  10. Event 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

  11. 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” • Let’s look behind the scenes and determine what the compiler is doing to our code.

  12. A foreach Enumeration in C# Cart c = new Cart(); foreach (string s in c) { lblResults.Text = lblResults.Text + s + "\n"; } Programmer Created Code Cart c = new Cart(); IEnumerator e = c.GetEnumerator(); while (e.MoveNext()) { string s = (string) e.Current; lblResults.Text = lblResults.Text + s + "\n"; } C# Compiler Generated Code

  13. ResultingSequence Diagram

More Related