// Iterator pattern -- Structural example
using System;
using System.Collections;
// "Aggregate"
abstract class Aggregate
{
// Methods
public abstract Iterator CreateIterator();
}
// "ConcreteAggregate"
class ConcreteAggregate : Aggregate
// Fields
private ArrayList items = new ArrayList();
public override Iterator CreateIterator()
return new ConcreteIterator( this );
// Properties
public int Count
get{ return items.Count; }
// Indexers
public object this[ int index ]
get{ return items[ index ]; }
set{ items.Insert( index, value ); }
// "Iterator"
abstract class Iterator
public abstract object First();
public abstract object Next();
public abstract bool IsDone();
public abstract object CurrentItem();
// "ConcreteIterator"
class ConcreteIterator : Iterator
private ConcreteAggregate aggregate;
private int current = 0;
// Constructor
public ConcreteIterator( ConcreteAggregate aggregate )
this.aggregate = aggregate;
override public object First()
return aggregate[ 0 ];
override public object Next()
if( current < aggregate.Count-1 )
return aggregate[ ++current ];
else
return null;
override public object CurrentItem()
return aggregate[ current ];
override public bool IsDone()
return current >= aggregate.Count ? true : false ;
/// <summary>
/// Client test
/// </summary>
public class Client
public static void Main(string[] args)
ConcreteAggregate a = new ConcreteAggregate();
a[0] = "Item A";
a[1] = "Item B";
a[2] = "Item C";
a[3] = "Item D";
// Create Iterator and provide aggregate
ConcreteIterator i = new ConcreteIterator(a);
// Iterate over collection
object item = i.First();
while( item != null )
Console.WriteLine( item );
item = i.Next();
Item AItem BItem CItem D
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.
// Iterator pattern -- Real World example
class Item
string name;
// Constructors
public Item( string name )
this.name = name;
public string Name
get{ return name; }
abstract class AbstractCollection
abstract public Iterator CreateIterator();
class Collection : AbstractCollection
return new Iterator( this );
set{ items.Add( value ); }
abstract class AbstractIterator
abstract public Item First();
abstract public Item Next();
abstract public bool IsDone();
abstract public Item CurrentItem();
class Iterator : AbstractIterator
private Collection collection;
private int step = 1;
public Iterator( Collection collection )
this.collection = collection;
public int Step
get{ return step; }
set{ step = value; }
override public Item First()
current = 0;
return (Item)collection[ current ];
override public Item Next()
current += step;
if( !IsDone() )
override public Item CurrentItem()
return current >= collection.Count ? true : false ;
/// IteratorApp test
public class IteratorApp
// Build a collection
Collection collection = new Collection();
collection[0] = new Item( "Item 0" );
collection[1] = new Item( "Item 1" );
collection[2] = new Item( "Item 2" );
collection[3] = new Item( "Item 3" );
collection[4] = new Item( "Item 4" );
collection[5] = new Item( "Item 5" );
collection[6] = new Item( "Item 6" );
collection[7] = new Item( "Item 7" );
collection[8] = new Item( "Item 8" );
// Create iterator
Iterator iterator = new Iterator( collection );
// Skip every other item
iterator.Step = 2;
// For loop using iterator
for( Item item = iterator.First();
!iterator.IsDone(); item = iterator.Next() )
Console.WriteLine( item.Name );
Item 0Item 2Item 4Item 6Item 8