Iterator

Frequency of Use
High

Intent
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Structure

Iterator

Iterator

Aplicability
Use the Iterator pattern

  • to access an aggregate object’s contents without exposing its internal representation.
  • to support multiple traversals of aggregate objects.
  • to provide a uniform interface for traversing different aggregate structures

Code Implementation (C#)

using System;
using System.Collections;

namespace Iterator
{
    class MainClass
    {

        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";
            
            ConcreteIterator i = new ConcreteIterator(a);
            object item = i.First();
            while(item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }

            Console.ReadKey();

        }

    }

    abstract class Aggregate
    {
        public abstract Iterator CreateIterator();
    }

    class ConcreteAggregate : Aggregate
    {
        ArrayList _items = new ArrayList();

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

        public int Count
        {
            get{return _items.Count;}
        }

        public object this [int index]
        {
            get{return _items[index];}
            set{_items.Insert(index,value);}
        }

    }

    abstract class Iterator 
    {
        public abstract object First();
        public abstract object Next();
        public abstract object CurrentItem();
        public abstract bool IsDone();
    }

    class ConcreteIterator : Iterator
    {
        private ConcreteAggregate _aggregate;
        private int _currentIndex = 0;

        public ConcreteIterator(ConcreteAggregate a)
        {
            this._aggregate = a;
        }

        public override object First()
        {
            return _aggregate[0];
        }

        public override object Next()
        {
            object _ret = null;
            if(_currentIndex < (_aggregate.Count - 1))
            {
                _currentIndex = _currentIndex + 1;
                _ret = _aggregate[_currentIndex];
            }

            return _ret;
        }

        public override object CurrentItem()
        {
            return _aggregate[_currentIndex];
        }

        public override bool IsDone()
        {
            return _currentIndex >= _aggregate.Count;
        }
    }
}

Output Code

Item A
Item B
Item C
Item D

 

Advertisements
Categories: C#, Design Pattern, Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: