Iterator Design Pattern

 UML diagram
 sample code in C#


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

Frequency of use:   high

UML class diagram


    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

   Better Code 
   Better Career 
   Better Lifestyle 

Design Pattern
Framework 4.5

C# and VB.NET


  Gang of Four Patterns
  Head First Patterns
  Enterprise Patterns
  Multi-Tier Patterns

  Repository Pattern
  Unit-of-Work Pattern
  CQRS Pattern
  Active Record Pattern

  Model View Controller
  Model View Presenter
  Model View ViewModel

  SparkTM RAD Platform
  Art Shop MVC Application
  Much more...

Click here for details

-- Instant Access --
Instant Download