迭代器模式(c++實現)

迭代器模式

模式定義

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

王二麻子 請出示您的車票!

張二蛋子 請出示您的車票!

李二狗子 請出示您的車票!

王小花兒 請出示您的車票!

優點

迭代器模式的優點

  • 迭代器模式就是分離了集合對象的遍歷行爲,抽象出一個迭代器類來負責,這樣既可以做到不暴露集合的內部結構,又可以讓外部代碼透明地訪問集合內部的數據。

缺點

模式的缺點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章