迭代器模式

问题描述:

       使用C++实现迭代器

 

迭代器模式(Iterator):

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

 

使用情况:

      需要访问一个聚合对象,而且不管这些对象是什么,都需要遍历的时候,考虑使用迭代器模式

 

如何使用:

       image

 

具体实现:

 

Aggregate.h 包含聚合对象基本组成部分

#ifndef AGGREGATE_H
#define AGGREGATE_H

/**
*    聚合对象基本组成部分
*/
class Aggregate
{
private:
    int value;

public:

    Aggregate(int val=0):value(val){}

    Aggregate(const Aggregate& agg)
    {
        this->value=agg.value;
    }

    Aggregate& operator=(const Aggregate& agg)
    {
        if (&agg!=this)
        {
            this->value=agg.value;
        }
        return *this;
    }

    int GetValue()
    {
        return value;
    }
};

#endif

 

 

CAggregate.h 聚合对象集合:

#ifndef   CAGGREATE_H
#define   CAGGREATE_H

/**
*    具体聚合对象集合
*/
#include "Aggregate.h"
#include "MyIterator.h"
#include <vector>
using namespace std;

class CAggregate
{
private:
    int count;
    vector<Aggregate> *container;

public:
    CAggregate()
    {
        count=0;
        container=new vector<Aggregate>(12);
    }

    CAggregate(const CAggregate& copyCAgg)
    {
        this->operator=(copyCAgg);
    }

    const CAggregate& operator=(const CAggregate& same)
    {
        if (&same!=this)
        {
            delete container;
            container=new vector<Aggregate>(12);
            for (int i=0;i<same.Count();i++)
            {
                Add(same[i]);
            }
        }
        return *this;
    }

    Aggregate operator[](const int index) const
    {
        Aggregate agg=NULL;
        if (index < (container->size()))
        {
            agg=container->at(index);
        }
        return agg;
    }

    void Add(const Aggregate& agg)
    {
        container->insert(container->begin(),agg);
        count++;
    }

    int  Count() const
    {
        return count;
    }
    
    ~CAggregate()
    {
        delete container;
    }
};

#endif

 

MyIterator.h 迭代器抽象类:

#ifndef MYITERATOR_H
#define MYITERATOR_H

/**
*    Iterator 抽象类
*/
#include "Aggregate.h"
class MyIterator
{
public:
    virtual Aggregate  GetCurrent()=0 ;    //纯虚函数,具体实现在派生类中
    virtual bool Next()=0;

    virtual ~MyIterator(){}
};

#endif

 

CIterator.h 具体迭代器类:

#ifndef  CITERATOR_H
#define     CITERATOR_H

/**
*    具体的Iterator
*/
#include "CAggregate.h"
#include "MyIterator.h"

class CIterator :public MyIterator
{
private:
    CAggregate ag;
    int count;
    int cur;

public:
    CIterator(const CAggregate& agg)
    {
        ag=agg;
        count=ag.Count();
        cur=0;
    }

    Aggregate GetCurrent()
    {
        if (cur<count)
        {
            return ag[cur];
        }
        return NULL;
    }

    bool Next()
    {
        if (++cur<count)
        {
            return true;
        }
        return false;
    }

    ~CIterator()
    {
    }
};

#endif

 

main.cpp测试函数:

#include "Aggregate.h"
#include "CAggregate.h"
#include "CIterator.h"

#include <iostream>
#include <string>
#include <list>
using namespace std;


int main()
{
    CAggregate cagg;
    for (int i=0;i<30;i++)
    {
        Aggregate agg(i);
        cagg.Add(agg);
    }

    CIterator iter(cagg);
    cout<<iter.GetCurrent().GetValue()<<endl;

    while(iter.Next())
    {
        cout<<iter.GetCurrent().GetValue()<<endl;
    }
}
posted @ 2013-11-18 22:28  罗松超  阅读(461)  评论(0编辑  收藏  举报