A brief introduction to the iterator pattern in C++ design patterns

黄舟
Release: 2017-01-18 14:51:36
Original
1350 people have browsed it

Iterator pattern: Provides a method to sequentially access each element in an aggregate object without exposing the internal representation of the object. [DP]

Four roles:

Iterator abstract class: used to define abstract methods such as getting the start object, getting the next object, judging whether to the end, the current object, etc., unified interface .

ConcreteIterator Specific iterator class: Inherit Iterator and implement methods such as specific start object and next object.

Aggregate aggregation abstract class: Create iterator

ConcreteAggregate concrete aggregation class

Applicable occasions:

When you need to access an aggregate object, regardless of these What the object is, it only needs to be traversed.

When you need to traverse the collection in multiple ways, such as from front to back or from back to front.

Provides a unified structure for traversing different aggregation structures such as start, next, whether to end, and which item is currently.

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;
}
Copy after login

Iterator pattern implementation:

[code]//迭代器模式
#include <iostream>
#include <deque>
#include <string>
using std::string;
using std::deque;

//MyIterator迭代器抽象类
class MyIterator{
public:
    MyIterator(){}
    //得到开始对象
    virtual string First() = 0;
    //得到下一个对象
    virtual string  Next() = 0;
    //判断是否到结尾
    virtual bool IsDone() = 0;
    //当前对象
    virtual string currentItem() = 0;   
};

//抽象聚集类
class Aggregate{
public:
    Aggregate(){}
    //创建迭代器
    virtual MyIterator* createIterator() = 0;
private:
    friend class MyIterator;
};
//具体聚集类
class ConcreteAggregate: public Aggregate{
public:
    MyIterator* createIterator();
    int Count();
    void Add(string st);
    string This(int index);
private:
    friend class ConcreteIterator;
    //乘客队列
    deque<string> passengers;
};

//具体的迭代器类
class ConcreteIterator: public MyIterator{
public:
    ConcreteIterator(ConcreteAggregate);
    string First();
    string Next();
    bool IsDone();
    string currentItem();
private:
    //以具体的聚集类为友元类
    friend class ConcreteAggregate;
    ConcreteAggregate aggregate;
    int current = 0;  
};

//方法实现
//具体迭代器类
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;
}

//方法实现
//具体聚集类
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);
}
Copy after login

Summary: The iterator pattern separates the traversal behavior of collection objects and abstracts an iterator class to take charge. , so that the internal structure of the collection is not exposed, and external code can transparently access the data inside the collection.

The above is the content of a brief introduction to the iterator pattern in C++ design patterns. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!