1. 程式人生 > >Design Pattern - Iterator(C#)

Design Pattern - Iterator(C#)

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

Definition

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:

  • Iterator (AbstractIterator) 
    • Defines an interface for accessing and traversing elements
    •  
  •  
  • ConcreteIterator (Iterator) 
    • Implements the Iterator interface
    •   
    • Keeps track of the current position in the traversal of the aggregate
    •  
  •  
  • Aggregate (AbstractCollection) 
    • Defines an interface for creating an Iterator object
    •  
  •  
  • ConcreteAggregate (Collection) 
    • Implements the Iterator creation interface to return an instance of the proper ConcreteIterator
    •  

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.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Iterator Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections;    /// <summary>    /// Startup class for Structural Iterator Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            var a = new ConcreteAggregate();            a[0] = "Item A";            a[1] = "Item B";            a[2] = "Item C";            a[3] = "Item D";            // Create Iterator and provide aggregate            var i = new ConcreteIterator(a);            Console.WriteLine("Iterating over collection:");            object item = i.First();            while (item != null)            {                Console.WriteLine(item);                item = i.Next();            }        }        #endregion    }    /// <summary>    /// The 'Aggregate' abstract class    /// </summary>    internal abstract class Aggregate    {        #region Public Methods and Operators        /// <summary>        /// The create iterator.        /// </summary>        /// <returns>        /// The <see cref="Iterator"/>.        /// </returns>        public abstract Iterator CreateIterator();        #endregion    }    /// <summary>    /// The 'ConcreteAggregate' class    /// </summary>    internal class ConcreteAggregate : Aggregate    {        #region Fields        /// <summary>        /// The _items.        /// </summary>        private ArrayList items = new ArrayList();        #endregion        // Gets item count        #region Public Properties        /// <summary>        /// Gets the count.        /// </summary>        public int Count        {            get            {                return this.items.Count;            }        }        #endregion        // Indexer        #region Public Indexers        /// <summary>        /// The this.        /// </summary>        /// <param name="index">        /// The index.        /// </param>        /// <returns>        /// The <see cref="object"/>.        /// </returns>        public object this[int index]        {            get            {                return this.items[index];            }            set            {                this.items.Insert(index, value);            }        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The create iterator.        /// </summary>        /// <returns>        /// The <see cref="Iterator"/>.        /// </returns>        public override Iterator CreateIterator()        {            return new ConcreteIterator(this);        }        #endregion    }    /// <summary>    /// The 'Iterator' abstract class    /// </summary>    internal abstract class Iterator    {        #region Public Methods and Operators        /// <summary>        /// The current item.        /// </summary>        /// <returns>        /// The <see cref="object"/>.        /// </returns>        public abstract object CurrentItem();        /// <summary>        /// The first.        /// </summary>        /// <returns>        /// The <see cref="object"/>.        /// </returns>        public abstract object First();        /// <summary>        /// The is done.        /// </summary>        /// <returns>        /// The <see cref="bool"/>.        /// </returns>        public abstract bool IsDone();        /// <summary>        /// The next.        /// </summary>        /// <returns>        /// The <see cref="object"/>.        /// </returns>        public abstract object Next();        #endregion    }    /// <summary>    /// The 'ConcreteIterator' class    /// </summary>    internal class ConcreteIterator : Iterator    {        #region Fields        /// <summary>        /// The _aggregate.        /// </summary>        private ConcreteAggregate aggregate;        /// <summary>        /// The _current.        /// </summary>        private int current;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="ConcreteIterator"/> class.        /// </summary>        /// <param name="aggregate">        /// The aggregate.        /// </param>        public ConcreteIterator(ConcreteAggregate aggregate)        {            this.aggregate = aggregate;        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The current item.        /// </summary>        /// <returns>        /// The <see cref="object"/>.        /// </returns>        public override object CurrentItem()        {            return this.aggregate[this.current];        }        /// <summary>        /// The first.        /// </summary>        /// <returns>        /// The <see cref="object"/>.        /// </returns>        public override object First()        {            return this.aggregate[0];        }        /// <summary>        /// Gets whether iterations are complete.        /// </summary>        /// <returns>        /// The <see cref="bool"/>.        /// </returns>        public override bool IsDone()        {            return this.current >= this.aggregate.Count;        }        /// <summary>        /// The next.        /// </summary>        /// <returns>        /// The <see cref="object"/>.        /// </returns>        public override object Next()        {            object ret = null;            if (this.current < this.aggregate.Count - 1)            {                ret = this.aggregate[++this.current];            }            return ret;        }        #endregion    }}// Output:/*Iterating over collection:Item AItem BItem CItem D*/

 


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.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Iterator Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections;    /// <summary>    /// The 'Aggregate' interface    /// </summary>    internal interface IAbstractCollection    {        #region Public Methods and Operators        /// <summary>        /// The create iterator.        /// </summary>        /// <returns>        /// The <see cref="Iterator"/>.        /// </returns>        Iterator CreateIterator();        #endregion    }    /// <summary>    /// The 'Iterator' interface    /// </summary>    internal interface IAbstractIterator    {        #region Public Properties        /// <summary>        /// Gets the current item.        /// </summary>        Item CurrentItem { get; }        /// <summary>        /// Gets a value indicating whether is done.        /// </summary>        bool IsDone { get; }        #endregion        #region Public Methods and Operators        /// <summary>        /// The first.        /// </summary>        /// <returns>        /// The <see cref="Item"/>.        /// </returns>        Item First();        /// <summary>        /// The next.        /// </summary>        /// <returns>        /// The <see cref="Item"/>.        /// </returns>        Item Next();        #endregion    }    /// <summary>    /// Startup class for Real-World Iterator Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Build a collection            var 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            var iterator = new Iterator(collection) { Step = 2 };            // Skip every other item            Console.WriteLine("Iterating over collection:");            for (Item item = iterator.First(); !iterator.IsDone; item = iterator.Next())            {                Console.WriteLine(item.Name);            }        }        #endregion    }    /// <summary>    /// A collection item    /// </summary>    internal class Item    {        #region Fields        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Item"/> class.        /// </summary>        /// <param name="name">        /// The name.        /// </param>        public Item(string name)        {            this.Name = name;        }        #endregion        // Gets name        #region Public Properties        /// <summary>        /// Gets the name.        /// </summary>        public string Name { get; private set; }        #endregion    }    /// <summary>    /// The 'ConcreteAggregate' class    /// </summary>    internal class Collection : IAbstractCollection    {        #region Fields        /// <summary>        /// The items.        /// </summary>        private ArrayList items = new ArrayList();        #endregion        // Gets item count        #region Public Properties        /// <summary>        /// Gets the count.        /// </summary>        public int Count        {            get            {                return this.items.Count;            }        }        #endregion        // Indexer        #region Public Indexers        /// <summary>        /// The this.        /// </summary>        /// <param name="index">        /// The index.        /// </param>        /// <returns>        /// The <see cref="object"/>.        /// </returns>        public object this[int index]        {            get            {                return this.items[index];            }            set            {                this.items.Add(value);            }        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The create iterator.        /// </summary>        /// <returns>        /// The <see cref="Iterator"/>.        /// </returns>        public Iterator CreateIterator()        {            return new Iterator(this);        }        #endregion    }    /// <summary>    /// The 'ConcreteIterator' class    /// </summary>    internal class Iterator : IAbstractIterator    {        #region Fields        /// <summary>        /// The collection.        /// </summary>        private Collection collection;        /// <summary>        /// The current.        /// </summary>        private int current;        /// <summary>        /// The step.        /// </summary>        private int step = 1;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Iterator"/> class.        /// </summary>        /// <param name="collection">        /// The collection.        /// </param>        public Iterator(Collection collection)        {            this.collection = collection;        }        #endregion        #region Public Properties        /// <summary>        /// Gets the current item.        /// </summary>        public Item CurrentItem        {            get            {                return this.collection[this.current] as Item;            }        }        // Gets whether iteration is complete        /// <summary>        /// Gets a value indicating whether is done.        /// </summary>        public bool IsDone        {            get            {                return this.current >= this.collection.Count;            }        }        /// <summary>        /// Gets or sets the step.        /// </summary>        public int Step        {            get            {                return this.step;            }            set            {                this.step = value;            }        }        #endregion