.NET常用设计模式——迭代器模式
2007-12-23 19:48
495 查看
迭代器模式(Iterator)
封装多个元素
使用户正确使用
遍历内部内容
总结Iterator
使用标准接口遍历列表/集合
封装操作
提供后期操功能作扩展
应用实例
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace CSDesingPattern
{
public class Employee
{
private int EmpId = 0;
private string EmpName = "";
public int ID
{
get
{
return EmpId;
}
set
{
EmpId = value;
}
}
public string Name
{
get
{
return EmpName;
}
set
{
EmpName = value;
}
}
}
//集合类
public class Employees
{
private ArrayList arEmp = new ArrayList();
private int EmpCount;
public Employees()
{
SqlConnection cn = new SqlConnection
("server=localhost;database=northwind;uid=sa;pwd=windows");
SqlCommand cmd = new SqlCommand
("select EmployeeID,FirstName,BirthDate from Employees", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employee obj = new Employee();
obj.ID = dr.GetInt32(0);
obj.Name = dr.GetString(1);
arEmp.Add(obj);
}
EmpCount = arEmp.Count;
}
public Employee Item(int idx)
{
return (Employee)arEmp[idx];
}
public int Count
{
get
{
return EmpCount;
}
}
}
//迭代器实现类
public class EmployeesIterator
{
private int intCurrent;
private Employees mObj;
public EmployeesIterator(ref Employees obj)
{
intCurrent = 0;
mObj = obj;
}
public Employee MoveFirst()
{
intCurrent = 0;
return mObj.Item(intCurrent);
}
public Employee MoveLast()
{
intCurrent = mObj.Count - 1;
return mObj.Item(intCurrent);
}
public Employee MoveNext()
{
intCurrent = intCurrent + 1;
if (intCurrent >= mObj.Count)
{
intCurrent = mObj.Count - 1;
throw new Exception("索引超过下限");
}
return mObj.Item(intCurrent);
}
public Employee MovePrev()
{
//throw new Exception (intCurrent.ToString());
intCurrent = intCurrent - 1;
if (this.intCurrent == -1)//||this.intCurrent <=0)
{
intCurrent = 1;
throw new Exception("索引超过上限,当前为" + intCurrent.ToString());
}
return mObj.Item(intCurrent);
}
}
}
封装多个元素
使用户正确使用
遍历内部内容
总结Iterator
使用标准接口遍历列表/集合
封装操作
提供后期操功能作扩展
应用实例
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace CSDesingPattern
{
public class Employee
{
private int EmpId = 0;
private string EmpName = "";
public int ID
{
get
{
return EmpId;
}
set
{
EmpId = value;
}
}
public string Name
{
get
{
return EmpName;
}
set
{
EmpName = value;
}
}
}
//集合类
public class Employees
{
private ArrayList arEmp = new ArrayList();
private int EmpCount;
public Employees()
{
SqlConnection cn = new SqlConnection
("server=localhost;database=northwind;uid=sa;pwd=windows");
SqlCommand cmd = new SqlCommand
("select EmployeeID,FirstName,BirthDate from Employees", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employee obj = new Employee();
obj.ID = dr.GetInt32(0);
obj.Name = dr.GetString(1);
arEmp.Add(obj);
}
EmpCount = arEmp.Count;
}
public Employee Item(int idx)
{
return (Employee)arEmp[idx];
}
public int Count
{
get
{
return EmpCount;
}
}
}
//迭代器实现类
public class EmployeesIterator
{
private int intCurrent;
private Employees mObj;
public EmployeesIterator(ref Employees obj)
{
intCurrent = 0;
mObj = obj;
}
public Employee MoveFirst()
{
intCurrent = 0;
return mObj.Item(intCurrent);
}
public Employee MoveLast()
{
intCurrent = mObj.Count - 1;
return mObj.Item(intCurrent);
}
public Employee MoveNext()
{
intCurrent = intCurrent + 1;
if (intCurrent >= mObj.Count)
{
intCurrent = mObj.Count - 1;
throw new Exception("索引超过下限");
}
return mObj.Item(intCurrent);
}
public Employee MovePrev()
{
//throw new Exception (intCurrent.ToString());
intCurrent = intCurrent - 1;
if (this.intCurrent == -1)//||this.intCurrent <=0)
{
intCurrent = 1;
throw new Exception("索引超过上限,当前为" + intCurrent.ToString());
}
return mObj.Item(intCurrent);
}
}
}
相关文章推荐
- .NET常用设计模式——迭代器模式
- .NET开发中最常用的设计模式
- .NET常用设计模式——工厂模式
- .net常用的设计模式
- .NET设计模式(18):迭代器模式(Iterator Pattern)
- 步步为营 .NET 设计模式学习笔记 十一、Iterator(迭代器模式)
- .NET常用设计模式(一)
- .NET设计模式(18):迭代器模式(Iterator Pattern)(转)
- 步步为营 .NET 设计模式学习笔记 十一、Iterator(迭代器模式)
- .NET设计模式(18):迭代器模式(Iterator Pattern)
- 步步为营 .NET 设计模式学习笔记 十一、Iterator(迭代器模式)
- JS常用的设计模式(12)—— 迭代器模式
- .NET设计模式(18):迭代器模式(Iterator Pattern)
- .NET常用设计模式——概述
- .NET设计模式-迭代器模式(Iterator Pattern)
- .NET常用设计模式(二)
- Java设计模式之迭代器模式和组合模式
- 设计模式解密(10)- 迭代器模式
- 设计模式--迭代器模式
- iOS开发中常用的设计模式