Iterator Design Pattern

Jack Herer 110 Reputation points
2023-04-18T16:21:46.4733333+00:00

I need an example of iterator pattern. Thanks alot.

Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Rajkamal Gupta 225 Reputation points
    2023-04-18T16:23:00.6266667+00:00

    Here you go... don't hesitate to ask, if you have some questions.

    public class Program
        {
            public static void Main(string[] args)
            {
                // Create 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;
    
                Console.WriteLine("Iterating over collection:");
    
                for (Item item = iterator.First();
                    !iterator.IsDone; item = iterator.Next())
                {
                    Console.WriteLine(item.Name);
                }
    
                // Wait for user
    
                Console.ReadKey();
            }
        }
        
        // 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();
        }
    
        // The 'ConcreteAggregate' class
        public class Collection : IAbstractCollection
        {
            List<Item> items = new List<Item>();
    
            public Iterator CreateIterator()
            {
                return new Iterator(this);
            }
    
            // Gets item count
            public int Count
            {
                get { return items.Count; }
            }
    
            // Indexer
            public Item this[int index]
            {
                get { return items[index]; }
                set { items.Add(value); }
            }
        }
        
        // The 'Iterator' interface
        public interface IAbstractIterator
        {
            Item First();
            Item Next();
            bool IsDone { get; }
            Item CurrentItem { get; }
        }
    
        // The 'ConcreteIterator' class
        public class Iterator : IAbstractIterator
        {
            Collection collection;
            int current = 0;
            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)
                {
                    return collection[current] as Item;
                }
                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; }
            }
        }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.