Iterator Design Pattern

Iterator pattern

In object-oriented programming, the Iterator pattern is a design pattern in which iterators are used to aggregate object sequentially without exposing its underlying representation. An Iterator object encapsulates the internal structure of how the iteration occurs.

This article, explains how to use the Iterator pattern to manipulate any collection of objects. To explain this I am using two interfaces IEnumerator and IEnumerables.

I have a class called Employee, which stores his ID and name.

  1. public class Employee  
  2. {  
  3.     private int m_nID;  
  4.     private string m_strName;  
  5.     public Employee(int nID,string strName)  
  6.     {  
  7.         m_nID = nID;  
  8.         m_strName = strName;  
  9.     }  
  10.     #region Property  
  11.     /// <summary>  
  12.     ///  
  13.     /// </summary>  
  14.     public int EmployeeID  
  15.     {  
  16.         get  
  17.             {  
  18.                 return this.m_nID;  
  19.             }  
  20.      }  
  21.     /// <summary>  
  22.     ///  
  23.     /// </summary>  
  24.     public string EmployeeName  
  25.     {  
  26.         get  
  27.             {  
  28.             return this.m_strName;  
  29.             }  
  30.         }  
  31.     # endregion  
  32. }  
I have two more classes
  1. EmployeeList, which is derived from the interface IEnumerable. So this class should implement GetEnumerator function. This function returns an object of  EnumerateEmployee which is derived from IEnumerator.
  2. EnumerateEmployee, which is derived from the interface IEnumerator. So this class should implement two functions namely MoveNext and Reset and a property called Current. In this class I created four objects of Employee objects and added to an array list. MoveNext function will iterate through this collection.

  1. public class EnumerateEmployee: IEnumerator  
  2. {  
  3.     private int m_nPosition;  
  4.     private ArrayList m_ArrEmployee = new ArrayList();  
  5.     /// <summary>  
  6.     ///  
  7.     /// </summary>d  
  8.     public EnumerateEmployee()  
  9.     {  
  10.         m_nPosition = -1;  
  11.         m_ArrEmployee.Add(new Employee(1,"Kamsa"));  
  12.         m_ArrEmployee.Add(new Employee(2,"Rama"));  
  13.         m_ArrEmployee.Add(new Employee(3,"Sita"));  
  14.         m_ArrEmployee.Add(new Employee(4,"Gopala"));  
  15.     }  
  16.     #region EnumMembers  
  17.     /// <summary>  
  18.     ///  
  19.     /// </summary>  
  20.     /// <returns></returns>  
  21.     public bool MoveNext()  
  22.     {  
  23.         bool b_return = false;  
  24.         ++m_nPosition;  
  25.         if(m_nPosition < m_ArrEmployee.Count)  
  26.         b_return = true;  
  27.         return b_return;  
  28.     }  
  29.     /// <summary>  
  30.     ///  
  31.     /// </summary>  
  32.     public object Current  
  33.     {  
  34.         get  
  35.         {  
  36.         return m_ArrEmployee[m_nPosition];  
  37.         }  
  38.     }  
  39.     /// <summary>  
  40.     ///  
  41.     /// </summary>  
  42.     public void Reset()  
  43.     {  
  44.         m_nPosition = -1;  
  45.     }  
  46. # endregion  
  47. }  

In order to execute this class, create a window application and put the following code in the button click event.

  1. string strName;  
  2. int nID;  
  3. EmployeeList objEmpList = new EmployeeList();  
  4. IEnumerator objEnumEmp = objEmpList.GetEnumerator();  
  5.   
  6. while(objEnumEmp.MoveNext())  
  7. {  
  8.     Employee objEmployee = (Employee)objEnumEmp.Current;  
  9.     nID = objEmployee.EmployeeID; strName = objEmployee.EmployeeName;  
  10. }