Design mode 17:iterator iterator mode (behavioral mode)

Source: Internet
Author: User

Iterator iterator Mode (behavioral mode)

Motive (motivation)

In the process of software building, the internal structure of the collection object is often different. But for these collection objects, we want to allow external client code to transparently access the elements it contains while not exposing its internal structure, and this "transparent variable" provides the possibility that the same algorithm operates on multiple collection objects.

Using object-oriented techniques to abstract this traversal mechanism into an "iterator object" provides an elegant way of "coping with changing collection objects."

Intentions (Intent)
Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation. --"Design pattern" GoF

Structure (Structure)

Sample code

    Abstract classIterator// abstract iterator  { Public Abstract ObjectFirst ();  Public Abstract ObjectNext ();  Public Abstract BOOLIsDone ();  Public Abstract ObjectCurrentItem (); }    Abstract classAggregate// Abstract Aggregation class  { Public AbstractIterator Createiterator ();//Creating Iterators    }    classconcreteaggregate:aggregate// Specific aggregation class  {Privateilist<Object> items=Newlist<Object>();  Public OverrideIterator Createiterator () {return NewConcreteiterator ( This); }         Public intCount {Get{returnitems. Count; }        }        //declaring an indexer         Public Object  This[intIndex] {            Get{returnItems[index];} Set{items.        Insert (Index,value);} }    }    classconcreteiterator:iterator// specific iterator  {Privateconcreteaggregate Aggregate; Private intCurrent =0;  PublicConcreteiterator (concreteaggregate aggregate)//when initializing, the specific clustered object is passed in        {             This. aggregate =aggregate; }         Public Override ObjectFirst () {returnaggregate[0];//get the first object to gather        }         Public Override ObjectNext () {ObjectRET =NULL; Current++; if(Current <aggregate. Count) {ret=Aggregate[current]; }            returnret; }         Public Override BOOLIsDone () {returnCurrent >=aggregate.        Count; }         Public Override ObjectCurrentItem () {returnAggregate[current]; }    }

Client calls:

        Static voidMain (string[] args) {Concreteaggregate a=Newconcreteaggregate (); a[0] ="Big Bird"; a[1] ="Side Dishes"; a[2] ="Sofa"; a[3] ="Bench"; Iterator Iterator=NewConcreteiterator (a);  while(!iterator. IsDone ()) {Console.WriteLine (iterator.                CurrentItem ()); Iterator.            Next ();        } console.readkey (); }

Output:

Big Bird
Plutella
Sofa
Bench

Several points of iterator model

    • Iterative abstraction: Accesses the interior of an aggregated object without exposing its internal representation.
    • Iterative polymorphism: Provides a unified interface for traversing different collection structures, enabling the same algorithm to operate on different sets of structures.
    • The robustness of an iterator is considered: traversing the same time as changing the collection structure where the iterator is located can cause problems.

Reprint please specify the source:

Jesselzj
Source: http://jesselzj.cnblogs.com

Design mode 17:iterator iterator mode (behavioral mode)

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.