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.

public class Employee

{

    private int m_nID;

    private string m_strName;

 

    public Employee(int nID,string strName)

    {

        m_nID = nID;

        m_strName = strName;

    }

    #region Property

   

    /// <summary>

    ///

    /// </summary>

    public int EmployeeID

    {

        get

            {

                return this.m_nID;

            }

     }

 

    /// <summary>

    ///

    /// </summary>

    public string EmployeeName

    {

        get

            {

            return this.m_strName;

            }

        }

    # endregion

 }

 

a. 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.

b. 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: IEnumeratorIn order to execute this class, create a window application and put the following code in the button click event.

{

    private int m_nPosition;

    private ArrayList m_ArrEmployee = new ArrayList();

 

    /// <summary>

    ///

    /// </summary>d

    public EnumerateEmployee()

    {

        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"));

    }

 

    #region EnumMembers

    /// <summary>

    ///

    /// </summary>

    /// <returns></returns>

    public bool MoveNext()

    {

        bool b_return = false;

        ++m_nPosition;

        if(m_nPosition < m_ArrEmployee.Count)

        b_return = true;

        return b_return;

    }

 

    /// <summary>

    ///

    /// </summary>

    public object Current

    {

        get

        {

        return m_ArrEmployee[m_nPosition];

        }

    }

 

    /// <summary>

    ///

    /// </summary>

    public void Reset()

    {

        m_nPosition = -1;

    }

# endregion

}

 

 

while(objEnumEmp.MoveNext())

{

    Employee objEmployee = (Employee)objEnumEmp.Current;

    nID = objEmployee.EmployeeID; strName = objEmployee.EmployeeName;

}

string strName;
int nID;
EmployeeList objEmpList = new EmployeeList ();
IEnumerator objEnumEmp = objEmpList.GetEnumerator();

I have two more classes
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值