ARTICLE

Iterator Design Pattern

Posted by rajesh mg Articles | Design & Architecture February 06, 2007
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.
Download Files:
 
Reader Level:

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.

Public Class Employee

    Private m_nID As Integer

    Private m_strName As String

 

    Public Sub New(ByVal nID As Integer, ByVal strName As String)

        m_nID = nID

        m_strName = strName

    End Sub

 

    Public ReadOnly Property EmployeeID() As Integer

        Get

            Return Me.m_nID

        End Get

    End Property

 

    Public ReadOnly Property EmployeeName() As String

        Get

            Return Me.m_strName

        End Get

    End Property

End Class

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.

Public Class EnumerateEmployee

    Implements IEnumerator

    Private m_nPosition As Integer

    Private m_ArrEmployee As ArrayList = New ArrayList

 

    Public Sub New()

        m_nPosition = -1

        m_ArrEmployee.Add(New Employee(1, "Kamsa"))

        m_ArrEmployee.Add(New Employee(2, "Rama"))

        m_ArrEmployee.Add(New Employee(3, "Sita"))

        m_ArrEmployee.Add(New Employee(4, "Gopala"))

    End Sub

 

    Public Function MoveNext() As Boolean

        Dim b_return As Boolean = False

        System.Threading.Interlocked.Increment(m_nPosition)

        If m_nPosition < m_ArrEmployee.Count Then

            b_return = True

        End If

        Return b_return

    End Function

 

    Public ReadOnly Property Current() As Object

        Get

            Return m_ArrEmployee(m_nPosition)

        End Get

    End Property

 

    Public Sub Reset()

        m_nPosition = -1

    End Sub

End Class

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

Dim strName As String

Dim nID As Integer

Dim objEmpList As EmployeeList = New EmployeeList

Dim objEnumEmp As IEnumerator = objEmpList.GetEnumerator

While objEnumEmp.MoveNext

    Dim objEmployee As Employee = CType(objEnumEmp.Current, Employee)

    nID = objEmployee.EmployeeID

    strName = objEmployee.EmployeeName

End While

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor