C#面向对象设计模式纵横谈——18 Iterator迭代器模式

主讲:李建忠

来源:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/consyscourse/CsharpOOD.aspx

 

clip_image002

clip_image004

clip_image006

clip_image008

 

   1:  public interface IEnumerable
   2:  {
   3:      IEnumerator GetEnumerator();
   4:  }
   5:   
   6:  public interface IEnumerator
   7:  {
   8:      Object Current {get;}
   9:      bool Movenext();
  10:      void Reset();
  11:  }
  12:   
  13:  private class MyEnumerator: IEnumerator
  14:  {
  15:      int nIndex;
  16:      MyCollection collection;
  17:   
  18:      public MyEnumerator(MyCollection coll)
  19:      {
  20:          collection=coll;
  21:          nIndex=-1;
  22:      }
  23:   
  24:      public bool MoveNext()
  25:      {
  26:          nIndex++;
  27:          return (nIndex<collection.items.GetLength(0));
  28:      }
  29:   
  30:      public int Current
  31:      {
  32:          get
  33:          {
  34:              return (collection.items[nIndex]);
  35:          }
  36:      }
  37:   
  38:      public void Reset()
  39:      {
  40:          nIndex=-1;
  41:      }
  42:  }

 

clip_image010

clip_image012

clip_image014

你可能感兴趣的:(iterator)