C + + design pattern Shallow knowledge iterator mode

Source: Internet
Author: User
Iterator pattern: Provides a way to access individual elements in an aggregated object sequentially, without exposing the internal representation of the object. "DP"

Four characters:

Iterator iterator abstract class: Used to define the starting object, get the next object, determine whether to the end, the current object and other abstract methods, unified interface.

Concreteiterator specific iterator class: Inherit iterator, implement the concrete start object, the next object and so on.

Aggregate aggregation abstract class: Creating iterators

Concreteaggregate Specific Aggregation classes

Applicable occasions:

When you need to access a clustered object, and no matter what these objects are, you only need to traverse them.

There are several ways to iterate over the aggregation, such as from front to back or from back to front.

Provides a unified structure for traversing different aggregation structures, such as start, next, end, current, and so on.

Test Case:

[Code]int Main () {    concreteaggregate pa;    Pa. ADD ("Bigbird");    Pa. ADD ("pickles");    Pa. ADD ("luggage");    Pa. ADD ("foreigners");    Pa. ADD ("Innerstaff");    Pa. ADD ("thief");    Concreteiterator ITR (PA);    String temp = Itr.first ();    while (! Itr.isdone ())    {        std::cout << itr.currentitem () << ", Ticket, please\n";        Itr.next ();    }    return 0;}

Iterator Pattern implementations:

[code]//iterator mode # include <iostream> #include <deque> #include <string>using std::string;using std::    Deque;//myiterator iterator Abstract class class Myiterator{public:myiterator () {}//Get Start object virtual string first () = 0;    Get the next object, virtual string next () = 0;    Judge whether to end virtual bool IsDone () = 0;   The current object, virtual string CurrentItem () = 0; };//Abstract Aggregation Class Aggregate{public:aggregate () {}//Create iterator Virtual myiterator* createiterator () = 0;private:frien D class myiterator;};/    /Specific aggregation classes class Concreteaggregate:public aggregate{public:myiterator* createiterator ();    int Count ();    void Add (String st);    string this (int index);P rivate:friend class Concreteiterator; Passenger queue deque<string> passengers;};/    /Specific iterator Classes class Concreteiterator:public Myiterator{public:concreteiterator (Concreteaggregate);    string First ();    String Next ();    BOOL IsDone ();    String CurrentItem ();p rivate://With specific aggregation classes as friend Class Concreteaggregate; ConcreteaggrEgate Aggregate;  int current = 0; };//method Implementation//Specific iterator class Concreteiterator::concreteiterator (Concreteaggregate Cagg): Aggregate (Cagg), current ( CAgg.passengers.size ()-1) {}string Concreteiterator::first () {return aggregate. This (0);    }string Concreteiterator::next () {string temp;    --current; if (current >= 0) temp = aggregate.    This (current); return temp;} String Concreteiterator::currentitem () {return aggregate. This (current);} BOOL Concreteiterator::isdone () {return current >= 0? False:true;} Method implementation//Concrete aggregation class myiterator* Concreteaggregate::createiterator () {return (new Concreteiterator (*this));} int Concreteaggregate::count () {return passengers.size ();} void Concreteaggregate::add (String st) {Passengers.push_back (ST);} string concreteaggregate::this (int index) {return passengers.at (index);}

Summary: The iterator pattern separates the traversal behavior of the collection object, abstracting 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.

The above is the C + + design mode of simple knowledge of the content of the iterator mode, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.