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.

Slides:



Advertisements
Similar presentations
Lists: An internal look
Advertisements

LINQ and Collections An introduction to LINQ and Collections.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
CSCI 160 Midterm Review Rasanjalee DM.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
DEV340 Microsoft Visual C# Under the Covers: An In-Depth Look at C# 2.0 Anders Hejlsberg Distinguished Engineer Microsoft Corporation.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Enumeration Pattern.
Rx Framework Reactive Extensions for.Net and JavaScript Andrea Nasoni & Diego Guidi.
New Features in C# 2.0 Sergey Baidachni MCT, MCSD, MCDBA.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
1.
Templates and the STL.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Monads Steve Goguen. About Me Web Developer for Allied Building Supply  ASP.NET, SQL Server, LINQ, etc. Website:
Reactive Extensions (Rx) Explained Presenter: Kevin Grossnicklaus August 5 th, 2011.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
ILM Proprietary and Confidential -
Visual C# 2005: IDE Enhancements Dan Fernandez C# Product Manager
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a procedural program and an Event Driven Program. Students.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Advanced C#, part I Niels Hallenberg IT University of Copenhagen BAAAP – Spring 2009.
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
Tail Recursion l The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. l Tail.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
1 Working with Controls at Run Time. 2 Objectives You will be able to Add controls to a Windows form at run time. Modify controls at run time.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
1 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Advanced .NET Programming I 2nd Lecture
Classes and Objects.
Iterators and Comparators
Chapter 5: Programming with C#
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
Lecture 10: Collections.
Visual C# 2005: Language Enhancements
Chapter 11 Generic Collections
Getting Ready for Visual Studio 2005
Web Service.
CIS 199 Final Review.
Basic Collections.
Outline Software Development Activities
Software Design Lecture : 39.
CSC 205 – Java Programming II
GoTo IEnumerable considered harmful
5. OOP OOP © 2003 Microsoft.
Visual C# 2005: Language Enhancements
Presentation transcript:

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. In Microsoft’s 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

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.

Classes for our Example

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 // 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 } Implemention of IEnumerable Interface

EnumCart Class 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

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

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.

A foreach 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"; } Programmer Created Code C# Compiler Generated Code

Resulting Sequence Diagram