.NET常用设计模式——迭代器模式

迭代器模式(Iterator)
  • 封装多个元素
  • 使用户正确使用
  • 遍历内部内容

总结Iterator

  • 使用标准接口遍历列表/集合
  • 封装操作
  • 提供后期操功能作扩展

应用实例
.NET常用设计模式——迭代器模式

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

}


 

你可能感兴趣的:(迭代器模式)