Design Mode 3: IteratorPattern)

Source: Internet
Author: User

The Iterator mode is also called the Cursor mode. It is defined as: provides a method to access each element in a container object without exposing the internal details of the object. The iterator mode is related to containers. The access to container objects is designed to traverse algorithms.

The iterator mode consists of the following roles:

  1. Iterator: The Iterator role defines interfaces for accessing and traversing elements.
  2. Concrete Iterator: The Iterator role must implement the Iterator interface and record the current position in the traversal.
  3. Container role: the Container role is responsible for providing interfaces for creating specific iterator roles.
  4. Concrete Container: the interface for creating a specific iterator role for a specific Container role-This iterator role is related to the structure of the Container.

Intent: provides a method to access a collection object without exposing the internal details of the object.

Iterator mode in. NET

The Iterator Mode Implemented in. NET already exists for the clustering interface and Iterator interface, and IEnumerator plays the role of Iterator. The Code is as follows:

Public interface IEnumerator

{

Object Current

{

Get;

}

Void Reset ();

Bool MoveNext ();

}

IEnumerable assumes the abstract aggregation role and has only one GetEnumertor () method. This interface must be implemented if the set object needs to have the iterative traversal function.

Public interface IEnumerable

{

IEnumerator GetEnumerator ();

}

Next, let's take a look at the Online Shopping. The PersonsEnumerator class is an enumerator class.

Public class Persons: IEnumerable

{

Public string [] m_Names;

Public Persons (string [] names)

{

M_Names = new string [names. Length];

Names. CopyTo (m_Names, 0 );

}

Private string this [int index]

{

Get

{

Return m_Names [index];

}

Set

{

M_Names [index] = value;

}

}

Public IEnumerator GetEnumerator ()

{

Return new PersonsEnumerator (this );

}

}

Public class PersonsEnumerator: IEnumerator

{

Private int index =-1;

Private Persons p;

Public PersonsEnumerator (Persons p)

{

This. p = p;

}

Public bool MoveNext ()

{

If (index <p. m_Names.Length)

{

Return true;

}

Return false;

}

Public void Reset ()

{

Index =-1;

}

Public object Current

{

Get

{

Return p. m_Names [index];

}

}

}

In. NET2.0 and later versions, the yield return keyword makes implementation easier:

Public class Persons: IEnumerable

{

Private string [] m_names;

Public Persons (string [] names)

{

M_names = new string [names. Length];

Names. CopyTo (m_names, 0 );

}

Public IEnumerator GetEnumerator ()

{

Foreach (string name in m_names)

{

Yield return name;

}

}

}

In addition, Let's briefly introduce the yield Keyword:

The keyword yield is used in the iterator block to provide a value to the enumerated number object or send an iteration end signal. The iterator has two special statements: yield return <expression>;

Yield break;

Iterator Block

An iterator block is a code base with one or more yield statements. Any of the following three types of code blocks can be iterator blocks:

Method subject

Accessors

Operator subject

The Yield statement can only appear in the iterator block. This block can be used as a method, operator, or accessors. In this type of method, operators or accessors are subject to the following constraints:

  1. Insecure blocks are not allowed.
  2. Method, operator, or accessor parameter cannot be ref or out
  3. Yield statements cannot appear in anonymous methods.
  4. The Yield return statement cannot exist in the try block containing one or more catch clauses in the Catch Block.

The falling block of the Yield statement can generate two types of objects: IEnumerator and IEnumerable:

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.