Design Pattern-iterator pattern (iterator)

Source: Internet
Author: User
Design Pattern (18): iterator pattern (iterator)
Iterator)
Definition
Provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object. The longest foreach in.
Motivation
When you need to access a clustering object, and all these objects need to be traversed, or you need to perform aggregation in multiple ways, you should consider using the iterator mode. The iterator mode provides unified interfaces for different clustering structures, such as start, next, end, and current.
Iterator Structure
Iterator abstract class
Abstract   Class Iterator
{
Public   Abstract   Object First ();
Public   Abstract   Object Next ();
Public   Abstract   Bool Isdone ();
Public   Abstract   Object Currentitem ();
}

Aggregate abstract class

Abstract   Class Aggregate
{
Public Absrtact iterator createiterator ();
}

Concreteiterator: a specific iterator class that inherits iterator

Class Concreteiterator: iterator
{
Private Concreteaggregate aggregate;
Private   Int Current =   0 ;
Public Concreteiterator (concreteaggregate aggregate)
{
This . Aggregate = Aggregate;
}
Public   Override   Object First ()
{
Return Aggregate [ 0 ]; // Obtain the first clustering object.
}

Public   Override   Object Next ()
{
Object RET =   Null ;
Current ++ ;
If (Current < Aggregate. Count)
{
RET = Aggregate [current];
}
Return RET;
}
Public   Override   Bool Isdone ()
{
Return Current > = Aggregate. Count ?   True : False ;
}
Public   Override   Object Currentitem ()
{
Return Aggregate [current];
}
}

Concreteaggregate the aggregate class inherits Aggregate

Class Concreteaggregate: Aggregate
{
Private Ilist < Object > Items = New List < Object > ();
Public   Override Iterator createiterator ()
{
Return   New Concreteiterator ( This );
}
Public   Int Count
{
Get { Return Items. Count ;}
}
Public   Object   This [ Int Index]
{
Get { Return Items. Count [Index];}
Set {Items. insert (index, value )}
}
}

Client Code

Static   Void Main ( String [] ARGs)
{
Concreteaggregate = New Concreteaggregate ();
A [ 0 ] = " Mingming " ;
A [ 0 ] = " Lili " ;
Iterator I = New Concreteiterator ();
Object Item = I. First ();
While ( ! I. isdone ())
{
Console. writeline ( " {0} buy tickets! " , I. currentitem ());
I. Next ();
}
}

The iterator mode separates the traversal behavior of the set object and abstracts an iterator to take charge of it, so that the internal structure of the set is not exposed, it also allows external code to transparently access data in the collection.

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.