设计模式-迭代器模式

ⅰ亾dé卋堺 提交于 2020-02-03 04:50:18

迭代器(Iterator)模式的定义:提供一个对象来顺序访问聚合对象中的一系列数据,而不暴露聚合对象的内部表示。该模式在现代c++中已经过时。伪代码如下:

#include <QCoreApplication>
#include <iostream>
using namespace std;

template <typename T>
class Iterator{
public:
    virtual void first(){}
    virtual void next(){}
    virtual bool isDone()const{return true;}
    virtual T& current(){return value;}

private:
    int value;
};

template <typename T>
class MyCollection{
public:
    Iterator<T>GetIterator(){
        return value;
    }
private:
    Iterator<T> value;

};

template <typename T>
class CollectionIterator:public Iterator<T>{
public:
    CollectionIterator(const MyCollection<T>& c):mc(c){}
    void first(){}
    void next(){}
    bool isDone()const{}
    T& current(){}
private:
    MyCollection<T> mc;
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    MyCollection<int>mc;

    Iterator<int>it = mc.GetIterator();
    for(it.first();!it.isDone();it.next()){

    }
    return a.exec();
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!