设计模式-迭代器模式

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

在软件构建过程中,集合对象内部结构常常变化各异。但对于这些集合对象,我们希望在不暴露器内部结构的同时,可以让外部客户代码透明的访问其中包含元素,同时这种透明遍历也为哦那个一种算法在多种集合对象行进行操作提供了可能。


提示:以下是本篇文章正文内容,下面案例可供参考

一、模式定义

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

二、实例代码

基于面向对象的实现

template<typename T>
class Iterator
{
   
public:
    virtual void first() = 0;
    virtual void next() = 0;
    virtual bool isDone() const = 0;
    virtual T& currentItem() const = 0;
};


template<typename T>
class MyCollection
{
   

public:
    Iterator<T>* CreateIterator() 
    {
   

    }
};

template<typename T>
class CollectionIterator: public Iterator<T>
{
   
    MyCollection<T> mc;

    public:
    CollectionIterator(MyCollection<T>& c):mc(c){
   }

    void first() override{
   }
    void next() override{
   }
    bool isDone() const override{
   }
    T& currentItem() const override{
   }
}


void MyAlgoritm()
{
   
    MyCollection<int> mc;
    Iterator<int>* iter = mc.CreateIterator();

    for(iter.first(); !iter.isDone(); iter.next())
    {
   
        //do something with iter.currentItem()  
    }
}

三、 类图

在这里插入图片描述

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

相关推荐

  1. 设计模式——模式

    2023-12-25 07:52:04       53 阅读
  2. 设计模式(15):模式

    2023-12-25 07:52:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-25 07:52:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-25 07:52:04       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-25 07:52:04       82 阅读
  4. Python语言-面向对象

    2023-12-25 07:52:04       91 阅读

热门阅读

  1. 设计模式之工厂方法模式

    2023-12-25 07:52:04       61 阅读
  2. 手写爬虫框架

    2023-12-25 07:52:04       68 阅读
  3. FFmpeg常见命令行

    2023-12-25 07:52:04       52 阅读
  4. Vue3和Vue2的区别

    2023-12-25 07:52:04       49 阅读
  5. JVM介绍

    JVM介绍

    2023-12-25 07:52:04      46 阅读
  6. Spring中的组合模式

    2023-12-25 07:52:04       59 阅读
  7. 前端八股文(vue篇)

    2023-12-25 07:52:04       56 阅读
  8. git 命令的使用

    2023-12-25 07:52:04       50 阅读
  9. react优劣势

    2023-12-25 07:52:04       53 阅读