C++设计模式-迭代器模式
生活随笔
收集整理的這篇文章主要介紹了
C++设计模式-迭代器模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
基本概念
代碼與實例
?
基本概念
迭代器模式(Iterator):提供一種方法順序訪問一個聚合對象中各個元素,而又不暴露該對象的內部表示。
當你需要一個聚集對象,而且不管這些對象,而不管這些對象是什么都需要遍歷的時候,就應該考慮用迭代器模式;
當需要對聚集有多種方式遍歷的時候,可以考慮用迭代器模式。
UML圖如下(此圖來源于大話設計模式)
?
代碼與實例
程序運行截圖如下:
代碼如下:
Head.h
#ifndef HEAD_H #define HEAD_H#include <iostream> #include <string> #include <list> using namespace std;typedef string objectStr; class ConcreteAggregate; class ConcreteIterator;//Iterator迭代器類 接口 class Iterator{public:Iterator();virtual~Iterator();virtual objectStr firstObjectStr();virtual objectStr nextObjectStr();virtual bool isDone();virtual objectStr currentObjectStr(); };//Aggregate聚集類 接口 class Aggregate{public:virtual Iterator *createIterator();virtual ~Aggregate(); };//ConcreteIterator具體迭代器類 class ConcreteIterator: public Iterator{public:ConcreteIterator(ConcreteAggregate *aggregate);~ConcreteIterator();objectStr firstObjectStr();objectStr nextObjectStr();bool isDone();objectStr currentObjectStr();private:ConcreteAggregate *m_aggregate; //具體聚集對象int m_current; };//ConcreteAggregate具體聚集類 繼承 Aggregate class ConcreteAggregate: public Aggregate{public:~ConcreteAggregate();Iterator *createIterator();int getListCount();objectStr getItem(const int &sub);void addItem(objectStr item);private:list<objectStr> m_items; };#endif // HEAD_HHead.cpp
#include "Head.h"Iterator::Iterator() {}Iterator::~Iterator() {cout << "Iterator::~Iterator()" << endl; }objectStr Iterator::firstObjectStr() {return "雅喵蝶"; }objectStr Iterator::nextObjectStr() {return "雅喵蝶"; }bool Iterator::isDone() {return false; }objectStr Iterator::currentObjectStr() {return "雅喵蝶"; }Iterator * Aggregate::createIterator() {return nullptr; }Aggregate::~Aggregate() {cout << "Aggregate::~Aggregate()" << endl; }ConcreteIterator::ConcreteIterator(ConcreteAggregate *aggregate) {m_aggregate = aggregate;m_current = 0; }ConcreteIterator::~ConcreteIterator() {cout << "ConcreteIterator::~ConcreteIterator()" << endl;delete m_aggregate; }objectStr ConcreteIterator::firstObjectStr() {return m_aggregate->getItem(0); }objectStr ConcreteIterator::nextObjectStr() {m_current++;if(m_current >= m_aggregate->getListCount()){return m_aggregate->getItem(m_aggregate->getListCount() - 1);}return m_aggregate->getItem(m_current); }bool ConcreteIterator::isDone() {return m_current >= m_aggregate->getListCount() ? true : false; }objectStr ConcreteIterator::currentObjectStr() {return m_aggregate->getItem(m_current); }ConcreteAggregate::~ConcreteAggregate() {cout << "ConcreteAggregate::~ConcreteAggregate()" << endl;m_items.clear(); }Iterator * ConcreteAggregate::createIterator() {return new ConcreteIterator(this); }int ConcreteAggregate::getListCount() {return this->m_items.size(); }objectStr ConcreteAggregate::getItem(const int &sub) {int i = 0;for(list<objectStr>::iterator it = m_items.begin(); it != m_items.end(); it++){if(i == sub){return (*it);}i++;}return "雅喵蝶"; }void ConcreteAggregate::addItem(objectStr item) {m_items.push_back(item); }main.cpp
#include "Head.h"int main(int *argc, int *argv[]){ConcreteAggregate *a = new ConcreteAggregate();a->addItem("球球");a->addItem("腿腿");a->addItem("米線");a->addItem("閏土");a->addItem("豬小明");a->addItem("妹爺");a->addItem("蘑菇頭");a->addItem("鍋蓋");Iterator *i = new ConcreteIterator(a);while(!i->isDone()){cout << i->currentObjectStr() << "買票了!!!" << endl;i->nextObjectStr();}delete a;getchar();return 0; }?
總結
以上是生活随笔為你收集整理的C++设计模式-迭代器模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arduino笔记-数字输入(开关的正确
- 下一篇: C++笔记-使用std::funcion