Iterator mode: iterator pattern (converted from terrylee)

Source: Internet
Author: User

Overview

In object-oriented software design, we often encounter a class of collection objects. The internal structure of these collection objects may have various implementations, but in conclusion, there are only two points of attention: one is the internal data storage structure of the set, and the other is to traverse the data inside the set. One of the principles of object-oriented design is the single responsibility principle of classes. Therefore, we should try our best to break down these responsibilities and use different classes to assume different responsibilities.IteratorThe pattern separates the traversal behavior of the set object and abstracts an iterator class to take charge of it. This not only ensures that the internal structure of the set is not exposed, but also enables the externalCodeTransparent access to data in the collection.

Intention

Provides a method to access each element of an aggregate object in sequence.,The internal representation of the object does not need to be exposed.[GofDesign Patterns]

Structure chart

IteratorThe schema structure is as follows:


Figure1 IteratorSchema Structure

Examples in life

The iterator provides a way to access each element in a set object sequentially without exposing the internal representation of the object. In early television sets, a dial was used to change the channel. When changing the channel, you need to manually turn the dial to move through each channel, regardless of whether the channel has a signal. Current TV set, use[Next]And[Previous]Button. When you press[Next]The button will switch to the next preset channel. Imagine watching TV in a hotel in a strange city. When a channel is changed, it is not the channels but the program content. If you are not interested in a channel program, you can switch to another channel without knowing how many channels it is.

Figure2 Using the selector as an ExampleIteratorPattern object Diagram

IteratorMode description

In object-oriented software design, we often encounter a class of collection objects. The internal structure of these collection objects may have various implementations, but in conclusion, there are only two points of attention: one is the internal data storage structure of the set, and the other is to traverse the data inside the set. One of the principles of object-oriented design is the single responsibility principle of classes. Therefore, we should try our best to break down these responsibilities and use different classes to assume different responsibilities.IteratorThe pattern separates the traversal behavior of the set object and abstracts an iterator class to take charge of it, so that the internal structure of the set is not exposed, in addition, external code can transparently access the internal data of the set. The following is a simple schematic example. The class structure is as follows:

Figure3Sample Code Structure

First, there is an abstract aggregation. The so-called aggregation is the collection of data, which can be accessed cyclically. It has only one method.Getiterator ()Implement the sub-classes to obtain an iterator object.

/**/ /// <Summary>

///Abstract Aggregation

/// </Summary>

Public   Interface Ilist

{
Iiterator getiterator ();
}

The abstract iterator is used to access clustering classes and encapsulate some methods to read the aggregated data in order. UsuallyMovenext (),Currentitem (),Fisrt (),Next ()And so on.

/**/ /// <Summary>

///Abstract iterator

/// </Summary>

Public   Interface Iiterator
{
BoolMovenext ();

Object currentitem ();

VoidFirst ();

VoidNext ();
}

The specific aggregation implements the unique method in the abstract aggregation and stores a set of data. Here we addLengthAttributes andGetelement ()The method is to facilitate access to the data in the aggregation.

/**/ /// <Summary>

///Specific Aggregation

/// </Summary>

Public   Class Concretelist: ilist
{
Int [] List;

Public Concretelist ()

{
List =   New   Int [] {1,2,3,4,5} ;
}

Public Iiterator getiterator ()

{
Return NewConcreteiterator (This);
}

Public   Int Length

{
Get {ReturnList. length ;}
}

Public   Int Getelement ( Int Index)

{
ReturnList [Index];
}
}

The specific iterator implements four methods in the abstract iterator. In its constructor, it needs to accept a specific clustering type parameter, here we can write different iteration methods based on the actual situation.

/**/ /// <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 ()

{< br> If (index list. length)
return true ;
else
return false ;
}

Public Object currentitem ()

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.