Design pattern 16: Iterator mode (Iterator)

Source: Internet
Author: User

Iterator mode:
Provides a way to sequentially access an aggregated object without leaking the underlying details of the object.
Provide a-from access the elements of an aggregate object sequentially without exposing its underlying representation.

In fact, this design pattern is used a lot, but the design is very few. Because the iterator in STL is the application of this pattern. The name of the design pattern is estimated to be used from an iterator in the STL.

UML diagram:

Mainly include:

    1. Iterator: Defines a series of interfaces that iterate through access elements
    2. Concreteiterator: Implements the interface defined by the iterator and saves the location of the elements traversed in a specific aggregate.
    3. Aggregate: Abstract Aggregate that defines an interface that returns iterator objects
    4. Concreteaggregate: Implements an interface that returns iterator.

Understanding this design pattern can be considered in conjunction with an iterator in the STL, so the role of the above roles is clear.
The key to implementing this design pattern with C + + is operator[] writing. We are equivalent to defining a simple container, which is exactly a container adapter.

C + + code:

#include <iostream>#include <vector>#include <string>#include <algorithm>using namespace STD;classIterator;classConcreteiterator;classaggregate{};//There is no known use of abstract aggregate because specific iterators need to know concreteaggregate//Internal implementation details, and in STL is ensured by the easy definition of an internal iterator class for each of theclassConcreteaggregate: Publicaggregate{ Public: Concreteaggregate () {datas= vector<string>( -); }friend classConcreteiterator;//This is actually a bit of a problem when the datas element is over 100 and there is a problem, and the memory needs to be reassigned                string&operator[](Const int&index) {returnDatas[index]; }Private: vector<string>Datas;};//Abstract iterator class that provides the following excuse, in which the meanings of these interfaces can be implemented in subclasses, such as forward in STL//And reverse iterators are different implementations of interfacesclassiterator{ Public:Virtual string&first () =0;Virtual stringNext () =0;Virtual BOOLIsDone () =0;Virtual string&currentitem () =0;};classConcreteiterator: Publiciterator{ Public: Concreteiterator (Concreteaggregate * agg) {Aggregate=agg; Cur=0; }string& First () {returnaggregate->datas[0]; }stringNext () {if(cur+1<aggregate->datas.size ()) {cur+=1;returnaggregate->datas[cur]; }Else{returnNULL; }                 }BOOLIsDone () {intLen=aggregate->datas.size ();if(cur==len-1)return true;Else                                return false; }string& CurrentItem () {returnaggregate->datas[cur]; }Private: Concreteaggregate * aggregate;intcur;};intMain () {STD::cout<<"iterator mode test"<<STD:: Endl;        Concreteaggregate agg; agg[0]="John"; agg[1]="Mike."; agg[2]="Bill."; agg[3]="Joe"; agg[4]="Kelly"; concreteiterator* iter=NewConcreteiterator (&agg);STD::cout<<iter->first () <<STD:: Endl; Iter->next ();STD::cout<<iter->currentitem () <<STD:: Endl;return 0;}

Execution output:

Design pattern 16: Iterator mode (Iterator)

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.