I understand the design pattern (C ++ implementation) -- iterator Pattern)

Source: Internet
Author: User
Overview:

In the current TV set, we can use the [next] and [previous] buttons to easily change the TV set. When you press the [next] button, it 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.

This is actually the essence of our iterator mode: it provides a method to access each element in an aggregate object sequentially without exposing the internal representation of the object.

Class diagrams and instances:

The iterator mode consists of the following roles:

1.Iterator role(Iterator): The iterator role defines interfaces for accessing and traversing elements.
2.Specific iterator role(Concrete iterator): The specific iterator role must implement the iterator interface and record the current position in the traversal.
3.Set roles(Aggregate): A collection role is responsible for providing interfaces for creating a specific iterator role.
4.Specific set roles(Concrete aggregate): the interface for creating a specific iterator role for a specific set role-the specific iterator role is related to the structure of the set.

#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<Item>*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;}

Implementation points:

1. Iterative Abstraction: access the content of an aggregate object without exposing its internal representation.

2. Iterative polymorphism: provides a unified interface for Traversing different set structures, so that the same algorithm can be operated on different set structures.

3. robustness of the iterator: Changing the collection structure of the iterator while traversing will cause problems.

Applicability:

1. Access the content of an aggregate object without exposing its internal representation.

2. Supports multiple traversal of aggregate objects.

3. provides a unified interface (supporting multi-state iteration) for Traversing different aggregation structures ).

Others:

1. See the implementation of STL iterators in C ++.

2. in. net implements the iterator mode. The clustering interface and the iterator interface already exist. ienumerator plays the role of the iterator, while ienumerable plays the role of abstract aggregation, she only has one getenumerator () method. If the collection object needs to have the fall generation traversal function, this interface must be implemented.

3. For details about Java, see java. util. iterator and Java. util. enumeration ).

Lcl_data was originally created in csdn. Net [http://blog.csdn.net/lcl_data/article/details/9310313]

For other design patterns, see:Design patterns I understand

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.