asp.net

asp.net,c#
行为型 Iterator模式(迭代)
Definition

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
UML class diagram
iterator[1].gif
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.
// 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();

 
// Methods
  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

{
  // Methods
  public abstract object First();
  public abstract object Next();
  public abstract bool IsDone();
  public abstract object CurrentItem();
}

// "ConcreteIterator"


class
ConcreteIterator : Iterator

{
  // Fields
  private ConcreteAggregate aggregate;
  private int current = 0;

 
// Constructor
  public ConcreteIterator( ConcreteAggregate aggregate )
  {
    this.aggregate = aggregate;
  }

 
// Methods
  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();
    }
  }
}

Output
Item A
Item B
Item C
Item 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

using System;
using System.Collections;
class Item
{
  // Fields
  string name;

 
// Constructors
  public Item( string name )
  {
    this.name = name;
  }

 
// Properties
  public string Name
  {
    get{ return name; }
  }
}

// "Aggregate"


abstract
class AbstractCollection

{
  abstract public Iterator CreateIterator();
}

// "ConcreteAggregate"


class
Collection : AbstractCollection

{
  // Fields
  private ArrayList items = new ArrayList();

 
// Methods
  public override Iterator CreateIterator()
  {
    return new Iterator( this );
  }

 
// Properties
  public int Count
  {
    get{ return items.Count; }
  }

 
// Indexers
  public object this[ int index ]
  {
    get{ return items[ index ]; }
    set{ items.Add( value ); }
  }
}

// "Iterator"


abstract
class AbstractIterator

{
  // Methods
  abstract public Item First();
  abstract public Item Next();
  abstract public bool IsDone();
  abstract public Item CurrentItem();
}

// "ConcreteIterator"


class
Iterator : AbstractIterator

{
  // Fields
  private Collection collection;
  private int current = 0;
  private int step = 1;

 
// Constructor
  public Iterator( Collection collection )
  {
    this.collection = collection;
  }

 
// Properties
  public int Step
  {
    get{ return step; }
    set{ step = value; }
  }

 
// Methods
  override public Item First()
  {
    current = 0;
    return (Item)collection[ current ];
  }

  override
public Item Next()

  {
    current += step;
    if( !IsDone() )
      return (Item)collection[ current ];
    else
      return null;
  }

  override
public Item CurrentItem()

  {
    return (Item)collection[ current ];
  }

  override
public bool IsDone()

  {
    return current >= collection.Count ? true : false ;
  }
}

///
<summary>

/// IteratorApp test
/// </summary>
public class IteratorApp
{
  public static void Main(string[] args)
  {
    // 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 );
    }
  }
}

Output
Item 0
Item 2
Item 4
Item 6
Item 8

posted on 2007-03-08 15:40  灵魂边缘  阅读(349)  评论(0编辑  收藏  举报