[C + + design mode] Iterator iterator mode

Source: Internet
Author: User

Iterator pattern definition: Provides a way to sequentially access individual elements in an aggregated object without exposing the object.

Iterators are internal iterators and external iterators, and internal iterators are tightly coupled with objects and are not recommended for use. External iterators are loosely coupled with the internal objects of the aggregation container, and are recommended for use.
The iterator pattern is the separation of the traversal behavior of the collection object and the abstraction of an iterator class to be responsible for not exposing the internal structure of the collection, but also allowing the external code to transparently access the data inside the collection. Moreover, you can simultaneously
Define multiple iterators to traverse, non-conflicting.

For iterators, referring to STL iterators, you can iterate through the aggregation objects within the container using only the iterator of the specific container, or you can implement objects such as additions, deletions, and invocations with an iterator.


1. Iterator role (Iterator): The iterator role is responsible for defining interfaces that access and traverse elements.
2. Specific iterator role (concrete Iterator): The specific iterator role is to implement the iterator interface and to record the current position in the traversal.
3. Collection Role (Aggregate): The collection role is responsible for providing the interface that creates the specific iterator role.
4. Specific collection roles (concrete Aggregate): The specific collection role implements the interface that creates the specific iterator role-the specific iterator role is related to the structure of the collection.

C + + iterator pattern (consistent with STL interface) code:

#include <iostream> #include <vector>using namespace Std;template<class item>class iterator{public:    virtual void First () = 0;    virtual void next () = 0;    Virtual item* currentitem () = 0;    virtual bool IsDone () = 0; Virtual ~iterator () {}};template<class Item>class concreteaggregate;template<class Item>class    Concreteiterator:public Iterator <item>{concreteaggregate<item> * AGGR;        int Cur;public:concreteiterator (CONCRETEAGGREGATE&LT;ITEM&GT;*A): Aggr (a), cur (0) {} virtual void first () {    cur=0;    } virtual void Next () {if (Cur<aggr->getlen ()) cur++;        } virtual item* CurrentItem () {if (Cur<aggr->getlen ()) Return & (*AGGR) [cur];    else return NULL;    } virtual bool IsDone () {return (Cur>=aggr->getlen ());    }};template<class item>class aggregate{public:virtual iterator<item>* createIterator () = 0; Virtual ~agGregate () {}};template<class item>class concreteaggregate:public aggregate<item>{vector<Item >        Data;public:concreteaggregate () {data.push_back (1);        Data.push_back (2);    Data.push_back (3);    } virtual iterator<item>* Createiterator () {return new concreteiterator<item> (this);    } item& operator[] (int index) {return data[index];    } int Getlen () {return data.size ();    }};int Main () {aggregate<int> * Aggr =new concreteaggregate<int> ();    Iterator<int> *it=aggr->createiterator (); For (It->first ();!    It->isdone (); It->next ()) {cout<<* (It->currentitem ()) <<endl;    } Delete it;    Delete Aggr; return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[C + + design mode] Iterator iterator 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.