行为型 Iterator模式(迭代)
Definition
UML class diagram
Participants
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.
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. |
Participants
The classes and/or objects participating in this pattern are:
|
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;
{ // Methods public abstract Iterator CreateIterator(); }
{ // 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 ); } } }
{ // Methods public abstract object First(); public abstract object Next(); public abstract bool IsDone(); public abstract object CurrentItem(); }
{ // Fields private ConcreteAggregate aggregate; private int current = 0;
// Constructor public ConcreteIterator( ConcreteAggregate aggregate ) { this.aggregate = aggregate; }
// Methods override public object First() { return aggregate[ 0 ]; }
{ if( current < aggregate.Count-1 ) return aggregate[ ++current ]; else return null; }
{ return aggregate[ current ]; }
{ return current >= aggregate.Count ? true : false ; } }
/// 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();
{ Console.WriteLine( item ); item = i.Next(); } } } |
|
Output
|
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; } } }
{ abstract public Iterator CreateIterator(); }
{ // 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 ); } } }
{ // Methods abstract public Item First(); abstract public Item Next(); abstract public bool IsDone(); abstract public Item CurrentItem(); }
{ // 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 ]; }
{ current += step; if( !IsDone() ) return (Item)collection[ current ]; else return null; }
{ return (Item)collection[ current ]; }
{ return current >= collection.Count ? true : false ; } }
/// 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
|