疊代器設計模式提供了一種按順序通路聚合對象的元素的方法,而不公開其基礎表示形式。
UML類圖
C# 中的結構代碼
#
此結構代碼示範了疊代器模式,該模式提供了一種周遊(疊代)項集合的方法,而無需詳細說明集合的基礎結構。
- using System;
- using System.Collections.Generic;
- namespace Iterator.Structural
- {
- /// <summary>
- /// Iterator Design Pattern
- /// </summary>
- public class Program
- 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
- Iterator i = a.CreateIterator();
- Console.WriteLine("Iterating over collection:");
- object item = i.First();
- while (item != null)
- Console.WriteLine(item);
- item = i.Next();
- }
- // Wait for user
- Console.ReadKey();
- /// The 'Aggregate' abstract class
- public abstract class Aggregate
- public abstract Iterator CreateIterator();
- /// The 'ConcreteAggregate' class
- public class ConcreteAggregate : Aggregate
- List<object> items = new List<object>();
- public override Iterator CreateIterator()
- return new ConcreteIterator(this);
- // Get item count
- public int Count
- get { return items.Count; }
- // Indexer
- public object this[int index]
- get { return items[index]; }
- set { items.Insert(index, value); }
- /// The 'Iterator' abstract class
- public abstract class Iterator
- public abstract object First();
- public abstract object Next();
- public abstract bool IsDone();
- public abstract object CurrentItem();
- /// The 'ConcreteIterator' class
- public class ConcreteIterator : Iterator
- ConcreteAggregate aggregate;
- int current = 0;
- // Constructor
- public ConcreteIterator(ConcreteAggregate aggregate)
- this.aggregate = aggregate;
- // Gets first iteration item
- public override object First()
- return aggregate[0];
- // Gets next iteration item
- public override object Next()
- object ret = null;
- if (current < aggregate.Count - 1)
- ret = aggregate[++current];
- return ret;
- // Gets current iteration item
- public override object CurrentItem()
- return aggregate[current];
- // Gets whether iterations are complete
- public override bool IsDone()
- return current >= aggregate.Count;
C# 中的實際代碼
此實際代碼示範了疊代器模式,該模式用于循環通路項集合,并在每次疊代中跳過特定數量的項。
- namespace Iterator.RealWorld
- // 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 = collection.CreateIterator();
- // Skip every other item
- iterator.Step = 2;
- for (Item item = iterator.First();
- !iterator.IsDone; item = iterator.Next())
- Console.WriteLine(item.Name);
- /// A collection item
- public class Item
- string name;
- public Item(string name)
- this.name = name;
- public string Name
- get { return name; }
- /// The 'Aggregate' interface
- public interface IAbstractCollection
- Iterator CreateIterator();
- public class Collection : IAbstractCollection
- List<Item> items = new List<Item>();
- public Iterator CreateIterator()
- return new Iterator(this);
- // Gets item count
- public Item this[int index]
- set { items.Add(value); }
- /// The 'Iterator' interface
- public interface IAbstractIterator
- Item First();
- Item Next();
- bool IsDone { get; }
- Item CurrentItem { get; }
- public class Iterator : IAbstractIterator
- Collection collection;
- int step = 1;
- public Iterator(Collection collection)
- this.collection = collection;
- // Gets first item
- public Item First()
- current = 0;
- return collection[current] as Item;
- // Gets next item
- public Item Next()
- current += step;
- if (!IsDone)
- else
- return null;
- // Gets or sets stepsize
- public int Step
- get { return step; }
- set { step = value; }
- // Gets current iterator item
- public Item CurrentItem
- get { return collection[current] as Item; }
- // Gets whether iteration is complete
- public bool IsDone
- get { return current >= collection.Count; }