Big talk design pattern C + + implementation-20th-iterator mode

Source: Internet
Author: User

First, UML diagram



Second, the concept

iterator Mode (Iterator): provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation.


Third, the description

When do you use it?

(1) When you need to access a clustered object, and no matter what they need to traverse, you should consider using the iterator pattern.

(2) When you need to have multiple traversal of the aggregation, you can consider using an iterator pattern.

(3) provides a unified interface for traversing different aggregation structures, such as start, next, end, current, and so on.

What are the benefits of the iterator pattern?

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.


Iv. implementation of C + +

(1) Iterator.h

#ifndef iterator_h#define iterator_h#include <vector> #include <iostream> #include "Aggregate.h" typedef std::string object;//Iterator Abstract class class Iterator{public:virtual object First () =0;virtual object Next () =0;virtual bool IsDone ( ) =0;virtual Object CurrentItem () =0;};/ /specific iterator class, from the previous iterator class Concreteiterator:public iterator{private:concreteaggregate* aggregate;int current;public: Concreteiterator (aggregate* Aggregate); object First (); object Next (); bool IsDone (); object CurrentItem ();};/ /specific iterator class, from the backward-forward iterator class Concreteiteratordesc:public iterator{private:concreteaggregate* aggregate;int current;public : Concreteiteratordesc (aggregate* Aggregate); object First (); object Next (); bool IsDone (); object CurrentItem ();}; #endif


(2) Iterator.cpp

#include "Iterator.h" Concreteiterator::concreteiterator (aggregate* Aggregate) {this->aggregate= ( concreteaggregate*) aggregate;current=0;} Object Concreteiterator::first () {return aggregate->getvector ()->at (0);} Object Concreteiterator::next () {current++;if (Current<aggregate->getvector ()->size ()) return aggregate- >getvector ()->at (current);} BOOL Concreteiterator::isdone () {return current>=aggregate->getvector ()->size ()? True:false;} Object Concreteiterator::currentitem () {return aggregate->getvector ()->at (current);} Concreteiteratordesc::concreteiteratordesc (aggregate* Aggregate) {this->aggregate= (concreteaggregate*) Aggregate;current= (((concreteaggregate*) aggregate)->getvector ()->size ())-1;} Object Concreteiteratordesc::first () {return * (Aggregate->getvector ()->end ());} Object Concreteiteratordesc::next () {current--;if (current>=0) return Aggregate->getvector ()->at (current);} BOOL Concreteiteratordesc::isdone () {return current<0?true:false;} ObJect Concreteiteratordesc::currentitem () {return aggregate->getvector ()->at (current);} 


(3) Aggregate.h

#ifndef aggregate_h#define aggregate_h#include <vector> #include <string> #include <iostream>class Iterator;class concreteiterator;typedef std::string object;//Aggregation abstract class Aggregate{public:virtual Iterator* Createiterator () = 0; Virtual std::vector<object>* getvector () =0;};/ /Specific aggregation classes class Concreteaggregate:public aggregate{private:std::vector<object> *items;public:concreteaggregate ( ); ~concreteaggregate ();//generates an iterator iterator* createiterator () from the back, and/or produces an iterator iterator* Createiteratordesc () from a backward forward; STD:: vector<object>* getvector (); int Count (), Object getelement (int index), void SetElement (int index,object o);}; #endif


(4) Aggregate.cpp

#include "Aggregate.h" #include "Iterator.h" Concreteaggregate::concreteaggregate () {items=new std::vector<object A.;} Concreteaggregate::~concreteaggregate () {delete items;} iterator* Concreteaggregate::createiterator () {iterator* it=new concreteiterator (this); return it;} iterator* Concreteaggregate::createiteratordesc () {iterator* it=new concreteiteratordesc (this); return it;} int Concreteaggregate::count () {return items->size ();} std::vector<object>* Concreteaggregate::getvector () {return items;} Object Concreteaggregate::getelement (int index) {return items->at (index);} void concreteaggregate::setelement (int index,object o) {items->at (index) =o;}


(5) Operation



Big talk design pattern C + + implementation-20th-iterator mode

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.