C++ iterator mode of design mode 16 (including sample code)

The Iterator pattern is a behavioral design pattern that provides a way to sequentially access the elements of an aggregate object without exposing the object's internal representation. The iterator pattern can be used to implement applications such as container classes and traversal algorithms.

Here is a sample code implementing the iterator pattern in C++:

#include <iostream>
#include <vector>
// 迭代器抽象类
class Iterator {
    
    
public:
    virtual ~Iterator() {
    
    }
    virtual int Next() = 0;
    virtual bool HasNext() = 0;
};
// 聚合抽象类
class Aggregate {
    
    
public:
    virtual ~Aggregate() {
    
    }
    virtual Iterator* CreateIterator() = 0;
};
// 具体迭代器类:向量迭代器
class VectorIterator : public Iterator {
    
    
public:
    VectorIterator(std::vector<int>& vector) : vector_(vector), index_(0) {
    
    }
    int Next() override {
    
    
        int value = vector_[index_];
        index_++;
        return value;
    }
    bool HasNext() override {
    
    
        return index_ < vector_.size();
    }
private:
    std::vector<int>& vector_;
    int index_;
};
// 具体聚合类:向量聚合
class VectorAggregate : public Aggregate {
    
    
public:
    VectorAggregate() {
    
    
        for (int i = 0; i < 10; i++) {
    
    
            vector_.push_back(i);
        }
    }
    Iterator* CreateIterator() override {
    
    
        return new VectorIterator(vector_);
    }
private:
    std::vector<int> vector_;
};
int main() {
    
    
    // 创建向量聚合对象
    VectorAggregate vector_aggregate;
    // 创建向量迭代器对象
    Iterator* iterator = vector_aggregate.CreateIterator();
    // 遍历向量
    while (iterator->HasNext()) {
    
    
        std::cout << iterator->Next() << " ";
    }
    std::cout << std::endl;
    // 释放迭代器对象
    delete iterator;
    return 0;
}

In the above code, we first define an Iterator abstract class, which contains the Next() and HasNext() pure virtual functions. The specific iterator class VectorIterator implements Next() and HasNext() functions to complete vector traversal. The aggregate abstract class Aggregate defines the CreateIterator() pure virtual function for creating iterators. The specific aggregation class VectorAggregate implements the CreateIterator() function to create a vector iterator.

In the main() function, we first create a vector aggregate object vector_aggregate, then create a vector iterator object iterator through the CreateIterator() function, and finally traverse the vector and output the result. After finishing the traversal, we free the iterator object.

The advantage of the iterator pattern is that it decouples the traversal algorithm from the aggregate object so that the internal representation of the aggregate object is not exposed. At the same time, the iterator mode also provides a general traversal interface, so that the traversal algorithm can be easily applied to different aggregation objects. However, since the iterator pattern needs to create iterator objects, it may increase the overhead of the system.

Guess you like

Origin blog.csdn.net/dica54dica/article/details/130021382