Iterator Design Pattern

 definition
 UML diagram
 participants
 sample code in C#



definition

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Frequency of use:   high

UML class diagram


participants

    The classes and/or objects participating in this pattern are:

  • Iterator  (AbstractIterator)
    • defines an interface for accessing and traversing elements.
  • ConcreteIterator  (Iterator)
    • implements the Iterator interface.
    • keeps track of the current position in the traversal of the aggregate.
  • Aggregate  (AbstractCollection)
    • defines an interface for creating an Iterator object
  • ConcreteAggregate  (Collection)
    • implements the Iterator creation interface to return an instance of the proper ConcreteIterator

sample code in C#

This structural code demonstrates the Iterator pattern which provides for a way to traverse (iterate) over a collection of items without detailing the underlying structure of the collection.

Show code

// Iterator pattern -- Structural example




This real-world code demonstrates the Iterator pattern which is used to iterate over a collection of items and skip a specific number of items each iteration.

Show code

// Iterator pattern -- Real World example




This .NET optimized code demonstrates the same real-world situation as above but uses modern, built-in .NET features, such as, generics, reflection, object initializers, automatic properties, etc.

Show code

// Iterator pattern -- .NET optimized




Need to know more?

    see what others are saying about the Iterator pattern





  • Better Code
  • Better Career
  • Better Lifestyle


Design Pattern
FrameworkTM 4.0


 
C# and VB.NET


     Here's what you get:
  • Gang of Four Patterns
  • Head First Patterns
  • Enterprise Patterns
  • SOA Patterns

  • WPF Best Practices
  • WCF Best Practices
  • LINQ Best Practices

  • Model View Controller
  • Model View Presenter
  • Model View ViewModel

  • More...

Click here for details

-- Instant Access --
Instant Download