惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
T
Tor Project blog
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
MongoDB | Blog
MongoDB | Blog
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
小众软件
小众软件
G
GRAHAM CLULEY
P
Privacy International News Feed
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
人人都是产品经理
人人都是产品经理
S
Schneier on Security
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
T
The Exploit Database - CXSecurity.com
Recent Announcements
Recent Announcements
E
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
The Register - Security
The Register - Security
S
Securelist
Martin Fowler
Martin Fowler
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog

博客园 - 鬼谷子com

2026年国内主流AI Coding Plan套餐全对比|开发者避坑指南 图解 | 你管这破玩意儿叫TCP?(转载) C++ static_cast、dynamic_cast、const_cast和reinterpret_cast(四种类型转换运算符) Qt内部的d指针和q指针手把手教你实现 C++中虚函数、虚继承内存模型 c++11新特性实战(二):智能指针 windows下使用mingw和msvc静态编译Qt5.15.xx Qt moc元对象编译器的原理和场景(反射) c++结构体内存对齐 c++11新特性实战 (一):多线程操作 抽象工厂模式(c++实现) 中介者模式(c++实现) 享元模式(c++实现) 代理模式(c++实现) 状态模式(c++实现) - 鬼谷子com 建造者模式(c++实现) 职责链模式(c++实现) 命令模式(c++实现) 模板方法模式(c++实现)
迭代器模式(c++实现)
鬼谷子com · 2020-07-20 · via 博客园 - 鬼谷子com

迭代器模式

模式定义

迭代器模式(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;
}
  • 运行结果

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

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

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

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

优点

迭代器模式的优点

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

缺点

模式的缺点