迭代器模式

前言

迭代器模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可让外部代码透明地访问集合内部的数据。

迭代器模式在访问数组、集合、列表等数据时,尤其是数据库数据操作时,是非常普遍的应用,但由于它太普遍了,所以各种高级语言都对它进行了封装,所以反而给人感觉此模式本身不太常用了。

代码

Aggregate.h

cpp 复制代码
#ifndef AGGREGATE_H
#define AGGREGATE_H

#include <string>
#include "Iterator.h"

using namespace std;

class Aggregate {
public:
    virtual ~Aggregate() = default;
    virtual int count() = 0;
    virtual void push(const string &strValue) = 0;
    virtual string pop(int idx) = 0;
    virtual Iterator *createIterator() = 0;
};
#endif //AGGREGATE_H

ConcreteAggregate.h

cpp 复制代码
#ifndef CONCRETEAGGREGATE_H
#define CONCRETEAGGREGATE_H

#include <vector>
#include "Aggregate.h"
#include "ConcreteIterator.h"

class ConcreteAggregate : public Aggregate {
public:
    ConcreteAggregate();
    ~ConcreteAggregate() override;
    Iterator *createIterator() override;
    int count() override;
    void push(const string &strValue) override;
    string pop(int idx) override;
private:
    vector <string> m_vecItems;
    Iterator *m_pIterator;
};

#endif //CONCRETEAGGREGATE_H

ConcreteAggregate.cpp

cpp 复制代码
#include "ConcreteAggregate.h"

ConcreteAggregate::ConcreteAggregate() : m_pIterator(nullptr) {
    m_vecItems.clear();
}

ConcreteAggregate::~ConcreteAggregate() {
    if (m_pIterator != nullptr) {
        delete m_pIterator;
        m_pIterator = nullptr;
    }
}

int ConcreteAggregate::count() {
    return m_vecItems.size();
}

void ConcreteAggregate::push(const string &strValue) {
    m_vecItems.push_back(strValue);
}

string ConcreteAggregate::pop(int idx) {
    string strRet;
    if (idx < count()) {
        strRet = m_vecItems[idx];
    }
    return strRet;
}

Iterator *ConcreteAggregate::createIterator() {
    if(m_pIterator == nullptr)
    {
        m_pIterator = new ConcreteIterator(this);
    }
    return m_pIterator;
}

Iterator.h

cpp 复制代码
#ifndef ITERATOR_H
#define ITERATOR_H

#include <string>

using namespace std;

class Iterator {
public:
    Iterator() = default;
    virtual ~Iterator() = default;
    virtual string first() = 0;
    virtual string next() = 0;
    virtual string getCurr() = 0;
    virtual bool isEnd() = 0;
};
#endif //ITERATOR_H

ConcreteIterator.h

cpp 复制代码
#ifndef CONCRETEITERATOR_H
#define CONCRETEITERATOR_H

#include "Iterator.h"
#include "Aggregate.h"

class ConcreteIterator : public Iterator {
public:
    explicit ConcreteIterator(Aggregate *pAggregate);
    string first() override;
    string next() override;
    string getCurr() override;
    bool isEnd() override;
private:
    Aggregate *m_Aggregate;
    int m_nCurrent;
};

#endif //CONCRETEITERATOR_H

ConcreteIterator.cpp

cpp 复制代码
#include "ConcreteIterator.h"

ConcreteIterator::ConcreteIterator(Aggregate *pAggregate) : m_nCurrent(0), Iterator() {
    m_Aggregate = pAggregate;
}

string ConcreteIterator::first() {
    return m_Aggregate->pop(0);
}

string ConcreteIterator::next() {
    string strRet;
    m_nCurrent++;
    if (m_nCurrent < m_Aggregate->count()) {
        strRet = m_Aggregate->pop(m_nCurrent);
    }
    return strRet;
}

string ConcreteIterator::getCurr() {
    return m_Aggregate->pop(m_nCurrent);
}

bool ConcreteIterator::isEnd() {
    return (m_nCurrent >= m_Aggregate->count());
}

main.cpp

cpp 复制代码
#include <iostream>
#include "iterator.h"
#include "ConcreteAggregate.h"

using namespace std;

int main() {
    auto *pName = new ConcreteAggregate();
    pName->push("hello");
    pName->push("word");
    pName->push("cue");
    Iterator *iter = pName->createIterator();
    if (iter != nullptr) {
        string strItem = iter->first();
        while (!iter->isEnd()) {
            cout << iter->getCurr() << " is ok" << endl;
            iter->next();
        }
    }
    return 0;
}
相关推荐
ErizJ3 天前
Golang | 迭代器模式
开发语言·golang·迭代器模式
Pasregret5 天前
迭代器模式:统一数据遍历方式的设计模式
设计模式·迭代器模式
Pasregret9 天前
迭代器模式:统一不同数据结构的遍历方式
数据结构·迭代器模式
搞不懂语言的程序员15 天前
迭代器模式深度解析与实战案例
开发语言·python·迭代器模式
hope_wisdom22 天前
实战设计模式之迭代器模式
设计模式·迭代器模式·软件工程·架构设计·软件架构
南宫生24 天前
Java迭代器【设计模式之迭代器模式】
java·学习·设计模式·kotlin·迭代器模式
牵牛老人1 个月前
C++设计模式-迭代器模式:从基本介绍,内部原理、应用场景、使用方法,常见问题和解决方案进行深度解析
c++·设计模式·迭代器模式
Hanson Huang1 个月前
23中设计模式-迭代器(Iterator)设计模式
java·设计模式·迭代器模式·行为型设计模式
Antonio9151 个月前
【Q&A】Qt有哪些迭代器模式的应用?
windows·qt·迭代器模式
Forget the Dream2 个月前
设计模式之迭代器模式
java·c++·设计模式·迭代器模式