迭代器模式(c++实现)

迭代器模式

模式定义

迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。

模式动机

  • 一个聚集对象,而且不管这些对象是什么都需要遍历的时候,你就应该考虑迭代器模式。
  • 你需要对聚集有多种方式遍历时,可以考虑用迭代器模式。
  • 为遍历不同的聚集结构提供如开始、下一个、是否结束、当前哪一项等统一的接口。

UML类图

源码实现

  • aggregate.h
#include <string>
#include <vector>

class Aggregate
{
public:
    Aggregate();
    void insert(std::string obj);
    virtual ~Aggregate();
    virtual int Count() = 0;
    virtual std::string& operator[](int i) = 0;

protected:
    std::vector<std::string>        m_Vec;
};
  • aggregate.cpp
#include "aggregate.h"

Aggregate::Aggregate()
{

}

Aggregate::~Aggregate()
{

}

void Aggregate::insert(std::string obj)
{
    m_Vec.push_back(obj);
}
  • iterator.h
#include "aggregate.h"

class Iterator
{
public:
    Iterator(Aggregate* aggregate);
    virtual ~Iterator();
    virtual std::string First() = 0;
    virtual std::string Next() = 0;
    virtual bool IsDone() = 0;
    virtual std::string CurrentItem() = 0;

protected:
    Aggregate*      m_Aggregate;
};
  • concreteIterator.h
#include "iterator.h"
#include "aggregate.h"

class ConcreteIterator : public Iterator
{
public:
    ConcreteIterator(Aggregate* aggredate);
    std::string First() override;
    std::string Next() override;
    bool IsDone() override;
    std::string CurrentItem() override;

private:
    int     m_CurrentIndex;
};
  • concreteIterator.cpp
#include <iostream>
#include "concreteiterator.h"

ConcreteIterator::ConcreteIterator(Aggregate* aggredate)
    :Iterator(aggredate)
{
    m_CurrentIndex = 0;
}

std::string ConcreteIterator::First()
{
    return (*m_Aggregate)[0];
}

std::string ConcreteIterator::Next()
{
    m_CurrentIndex++;
    if(m_CurrentIndex < m_Aggregate->Count())
        return (*m_Aggregate)[m_CurrentIndex];
    else
        return "";
}

bool ConcreteIterator::IsDone()
{
    return m_CurrentIndex >= m_Aggregate->Count() ? true : false;
}

std::string ConcreteIterator::CurrentItem()
{
    return (*m_Aggregate)[m_CurrentIndex];
}
  • concreteAggregate.h
#include <aggregate.h>
#include <string>
#include "iterator.h"

class ConcreteAggregate : public Aggregate
{
public:
    ConcreteAggregate();
    int Count() override;
    std::string &operator[](int i) override;

private:
    Iterator*       m_Iterator;
};

  • concreteAggregate.cpp
#include "concreteaggregate.h"

ConcreteAggregate::ConcreteAggregate()
{
}

int ConcreteAggregate::Count()
{
    return static_cast<int>(m_Vec.size());
}

std::string& ConcreteAggregate::operator[](int i)
{
    return m_Vec[size_t(i)];
}
  • main.cpp
#include <iostream>
#include "concreteaggregate.h"
#include "concreteiterator.h"
using namespace std;

int main()
{
    Aggregate* agt =  new ConcreteAggregate();
    agt->insert("王二麻子");
    agt->insert("张二蛋子");
    agt->insert("李二狗子");
    agt->insert("王小花儿");

    Iterator* i = new ConcreteIterator(agt);
    while(!i->IsDone())
    {
        std::cout << i->CurrentItem() << " 请出示您的车票!" << std::endl;
        i->Next();
    }
    return 0;
}
  • 运行结果

王二麻子 请出示您的车票!

张二蛋子 请出示您的车票!

李二狗子 请出示您的车票!

王小花儿 请出示您的车票!

优点

迭代器模式的优点

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

缺点

模式的缺点

posted @   鬼谷子com  阅读(738)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2016-07-20 linux-svn命令
点击右上角即可分享
微信分享提示