迭代器模式(Iterator)
创始人
2024-02-25 01:28:41
0

参考:

迭代器设计模式 (refactoringguru.cn)

[design-patterns-cpp/Iterator.cpp at master · JakubVojvoda/design-patterns-cpp · GitHub

文章目录

    • 一、什么是迭代器模式?
    • 二、实现
    • 三、优缺点
      • 优点
      • 缺点

一、什么是迭代器模式?

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

在这里插入图片描述

二、实现

在这里插入图片描述

迭代器(Iterator)模式包含以下主要角色:

1、抽象聚合类(Aggregate)角色:也有人把它叫集合。抽象聚合类用于存储对象,并定义创建相应迭代器对象的接口,声明一个createIterator方法用于创建一个迭代器对象。
2、具体聚合类(Concrete Aggregate)角色:具体聚合类实现了创建相应迭代器的接口,实现了在聚合类中声明的createIterator方法,该方法返回一个与该具体聚合对应的具体迭代器ConcreteIterator实例。
3、抽象迭代器(Iterator)角色:抽象迭代器定义了访问和遍历元素的接口,一般声明如下方法:用于获取第一个元素的first(),用于访问下一个元素的next(),用于判断是否还有下一个元素的hasNext(),用于获取当前元素的currentItem(),在其子类中将实现这些方法。
4、具体迭代器(Concrete Iterator)角色:具体迭代器实现了抽象迭代器接口,完成对聚合对象的遍历,同时在对聚合进行遍历时跟踪其当前对象。

/** C++ Design Patterns: Iterator* Author: Jakub Vojvoda [github.com/JakubVojvoda]* 2016** Source code is licensed under MIT License* (for more details see LICENSE)**/#include 
#include 
#include class Iterator;
class ConcreteAggregate;/** Aggregate* defines an interface for aggregates and it decouples your* client from the implementation of your collection of objects*/
class Aggregate
{
public:virtual ~Aggregate() {}virtual Iterator *createIterator() = 0;// ...
};/** Concrete Aggregate* has a collection of objects and implements the method* that returns an Iterator for its collection**/
class ConcreteAggregate : public Aggregate
{
public:ConcreteAggregate( const unsigned int size ){list = new int[size]();count = size;}~ConcreteAggregate(){delete[] list;}Iterator *createIterator();unsigned int size() const{return count;}int at( unsigned int index ){return list[ index ];}// ...private:int *list;unsigned int count;// ...
};/** Iterator* provides the interface that all iterators must implement and* a set of methods for traversing over elements*/
class Iterator
{
public:virtual ~Iterator() { /* ... */ }virtual void first() = 0;virtual void next() = 0;virtual bool isDone() const = 0;virtual int currentItem() const = 0;// ...
};/** Concrete Iterator* implements the interface and is responsible for managing* the current position of the iterator*/
class ConcreteIterator : public Iterator
{
public:ConcreteIterator( ConcreteAggregate *l ) :list( l ), index( 0 ) {}~ConcreteIterator() {}void first(){index = 0;}void next(){index++;}bool isDone() const{return ( index >= list->size() );}int currentItem() const{if ( isDone() ){return -1;}return list->at(index);}// ...private:ConcreteAggregate *list;unsigned int index;// ...
};Iterator *ConcreteAggregate::createIterator()
{return new ConcreteIterator( this );
}int main()
{unsigned int size = 5;ConcreteAggregate list = ConcreteAggregate( size );Iterator *it = list.createIterator();for ( ; !it->isDone(); it->next()){std::cout << "Item value: " << it->currentItem() << std::endl;}delete it;return 0;
}

Note

以上使用运行时多态实现的迭代器,以C++中已经用模板替换。当遍历次数较多时,由于访问虚函数表次数过多会导致性能下降,而编译时多态正好弥补了这一问题。

三、优缺点

优点

  • 使用该模式可以减少程序中重复的遍历代码。
  • 如果你希望代码能够遍历不同的甚至是无法预知的数据结构,可以使用迭代器模式。

缺点

  • 对于某些特殊集合, 使用迭代器可能比直接遍历的效率低。

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...