Design Pattern discovered in. Net Class Library: iterator Pattern

Source: Internet
Author: User

Iteration mode:

Provides a method to access each element in an aggregate object sequentially without exposing the internal representation of the object.

 

Applications in. Net:

 

 

/// <Summary>
/// Abstract Aggregation
/// </Summary>
Public interface ilist
{
Iiterator getiterator ();
}

/// <Summary>
/// Abstract iterator
/// </Summary>
Public interface iiterator
{
Bool movenext ();

Object currentitem ();

Void first ();

Void next ();
}

/// <Summary>
/// Specific aggregation
/// </Summary>
Public class concretelist: ilist
{
Int [] list;
Public concretelist ()
{
List = new int [] {1, 2, 3, 4, 5 };
}

Public iiterator getiterator ()
{
Return new concreteiterator (this );
}

Public int Length
{
Get {return list. length ;}
}

Public int getelement (INT index)
{
Return list [Index];
}
}

/// <Summary>
/// Specific iterator
/// </Summary>
Public class concreteiterator: iiterator
{
Private concretelist list;
Private int index;
Public concreteiterator (concretelist List)
{
This. List = List;
Index = 0;
}

Public bool movenext ()
{
If (index <list. length)
Return true;
Else
Return false;
}

Public object currentitem ()
{
Return list. getelement (INDEX );
}
Public void first ()
{
Index = 0;
}
Public void next ()
{
If (index <list. length)
{
Index ++;
}
}
}

/// <Summary>
/// Client program
/// </Summary>
Public class Program
{
Static void main (string [] ARGs)
{
Iiterator iterator;

Ilist list = new concretelist ();

Iterator = List. getiterator ();

While (iterator. movenext ())
{
Int I = (INT) iterator. currentitem ();
Console. writeline (I. tostring ());

Iterator. Next ();
}

Console. Read ();
}
}

 

First, there is an abstract aggregation ilist. The so-called aggregation is the set of data, which can be accessed cyclically. It has only one method getiterator () for sub-classes to implement and obtain an iterator object. The abstract iterator iiterator is used to access clustering classes and encapsulate some methods to read the aggregated data in order. There are several methods for sub-classes to implement movenext (), currentitem (), Fisrt (), and next. The specific aggregate concretelist implements the unique method in the abstract aggregation, and saves a set of data in it. Here we add the Length attribute and getelement () the method is to facilitate access to the data in the aggregation. Specific iterator concreteiterator
Implements the four methods in the abstract iterator. In its constructor, you need to accept a specific clustering type parameter, here we can write different iteration methods based on the actual situation.

Ienumerable and ienumerator are obviously in.. NET Framework encapsulates the iterator mode to enable the iteration class of ienumerator to be separated from the Collection class that implements ienumerable, this class is responsible for processing the enumeration State (including how to express the elements in the current array and how to iterate the elements in the set), and including the enumeration algorithm. This method allows you to enumerate the set iterator using different methods at the same time without adding any complexity to the set!

Public class persons: ienumerable
{
String [] m_names;
Public persons (Params string [] Names)
{
M_names = new string [names. Length];

Names. copyto (m_names, 0 );
}
Public ienumerator getenumerator ()
{
Foreach (string s in m_names)
{
Yield return S;
}
}
}

Public class Program
{
Static void main (string [] ARGs)
{
Persons arrpersons = new persons ("micel", "Christine", "Mathieu", "Julien ");
Foreach (string s in arrpersons)
{
Console. writeline (s );
}
Console. Readline ();
}
}

Reference: http://www.cnblogs.com/Terrylee/archive/2006/09/16/Iterator_Pattern.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.