The Iterator Design Pattern separates the logic to navigate a collection into a different layer, so your collections won't need to understand how to iterate through its elements.
This pattern exists in a class that will hold the elements and another class that will navigate.
An example from scratch could be a collection of stories that are iterated using a StoryIterator and not by the collection itself as in the following:
- public class StoryCollection
- {
- private ArrayList items = new ArrayList();
- public int Count
- {
- get
- {
- return items.Count;
- }
- }
- public Story this[int index]
- {
- get
- {
- return items[index] as Story;
- }
- set
- {
- items.Insert(index, value);
- }
- }
- public StoryEnumerator GetEnumerator()
- {
- return new StoryEnumerator(this);
- }
- }
- public class StoryEnumerator
- {
- private int currentIndex = -1;
- private StoryCollection collection;
- public StoryEnumerator(StoryCollection collection)
- {
- this.collection = collection;
- }
- public Story Current
- {
- get
- {
- if (currentIndex < 0 || collection.Count <= currentIndex)
- return null;
- return collection[currentIndex];
- }
- }
- public bool MoveNext()
- {
- if (collection.Count == (currentIndex + 1))
- return false;
- ++currentIndex;
- return true;
- }
- public void Reset()
- {
- currentIndex = -1;
- }
- }
- [TestClass]
- public class IteratorTest
- {
- [TestMethod]
- public void MoveNext_EmptyArray_DoesNothing()
- {
- var collection = new StoryCollection();
- var iterator = collection.GetEnumerator();
- while (iterator.MoveNext())
- {
- Assert.Fail();
- }
- }
- [TestMethod]
- public void MoveNext_TwoElements_AddsTwo()
- {
- int sum = 0;
- var collection = new StoryCollection();
- collection[0] = new Story();
- collection[1] = new Story();
- var iterator = collection.GetEnumerator();
- while (iterator.MoveNext())
- {
- sum++;
- }
- Assert.AreEqual(2, sum);
- }
- [TestMethod]
- public void MoveNextTwice_TwoElements_AddsTwo()
- {
- int sum = 0;
- var collection = new StoryCollection();
- collection[0] = new Story();
- collection[1] = new Story();
- var iterator = collection.GetEnumerator();
- while (iterator.MoveNext())
- {
- sum++;
- }
- while (iterator.MoveNext())
- {
- sum++;
- }
- Assert.AreEqual(2, sum);
- }
- [TestMethod]
- public void MoveNextTwiceThenReset_TwoElements_AddsFour()
- {
- int sum = 0;
- var collection = new StoryCollection();
- collection[0] = new Story();
- collection[1] = new Story();
- var iterator = collection.GetEnumerator();
- while (iterator.MoveNext())
- {
- sum++;
- }
- iterator.Reset();
- while (iterator.MoveNext())
- {
- sum++;
- }
- Assert.AreEqual(4, sum);
- }
- [TestMethod]
- public void MoveNext_TwoElements_GetsCurrent()
- {
- int i = 0;
- var collection = new StoryCollection();
- collection[0] = new Story { Title = "0" };
- collection[1] = new Story { Title = "1" };
- var iterator = collection.GetEnumerator();
- while (iterator.MoveNext())
- {
- Assert.AreEqual(i.ToString(), iterator.Current.Title);
- ++i;
- }
- Assert.AreEqual(2, i);
- }
- }

Sibeesh VenuPosted Jun 21, 2015, 12:51 PM
Good one.
Santhakumar MunuswamyPosted Jun 21, 2015, 2:02 AM
Thanks for nice article:)