c++设计模式----迭代器模式iterator
来源:互联网 发布:检测到您的网络异常 编辑:程序博客网 时间:2024/05/16 08:41
迭代器:
意图:
提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。
适用性:
访问一个聚合对象的内容而无需暴露它的内部表示。
意图:
提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。
适用性:
访问一个聚合对象的内容而无需暴露它的内部表示。
支持对聚合对象的多种遍历。
为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代)。
#include <iostream>#include <string>#include <vector>using namespace std;class Iterator{public:Iterator(){}virtual ~Iterator(){};virtual string First() = 0;virtual string Next() = 0;virtual string GetCur() = 0;virtual bool IsEnd() = 0;};class Aggregate{public:virtual int Count() = 0;virtual void Push(const string &strValue) = 0;virtual string POp(const int cnt) = 0;virtual Iterator* CreateIterator() = 0;};class ConcreteIterator : public Iterator{public:ConcreteIterator(Aggregate *p_aggregate):m_aggregate(p_aggregate),m_cnt(0){}string First(){return m_aggregate->POp(0);}string Next(){string str;m_cnt++;if (m_cnt < m_aggregate->Count()){str = m_aggregate->POp(m_cnt);}return str;}string GetCur(){return m_aggregate->POp(m_cnt);}bool IsEnd(){return m_cnt >= m_aggregate->Count() ? true : false;}~ConcreteIterator(){ }private:Aggregate *m_aggregate;int m_cnt;};class ConcreteAggregate : public Aggregate{public:ConcreteAggregate() :m_pIterator(NULL){m_vecItem.clear();}~ConcreteAggregate(){if (NULL != m_pIterator){delete m_pIterator;m_pIterator = NULL;}}int Count(){return m_vecItem.size();}void Push(const string &strValue){m_vecItem.push_back(strValue);}string POp(const int cnt){string str;if (cnt < Count()){str = m_vecItem[cnt];}return str;}Iterator* CreateIterator(){if (NULL == m_pIterator){m_pIterator = new ConcreteIterator(this);}return m_pIterator;}private:vector<string> m_vecItem;Iterator *m_pIterator;};int main(){ConcreteAggregate* pName = NULL;pName = new ConcreteAggregate();if (NULL != pName){pName->Push("hello");pName->Push("word");pName->Push("cxue");}Iterator* iter = NULL;iter = pName->CreateIterator();if (NULL != iter){string strItem = iter->First();while (!iter->IsEnd()){cout << iter->GetCur() << " is ok" << endl;iter->Next();}}system("pause");return 0;}
改进一下也许更好懂一点:
#include <iostream>#include <string>#include <vector>using namespace std;class Iterator{public:Iterator(){}virtual ~Iterator(){};virtual string First() = 0;virtual string Next() = 0;virtual string GetCur() = 0;virtual bool IsEnd() = 0;};class Aggregate{public:virtual int Count() = 0;virtual void Push(const string &strValue) = 0;virtual string POp(const int cnt) = 0;};class ConcreteIterator : public Iterator{public:ConcreteIterator(Aggregate *p_aggregate):m_aggregate(p_aggregate),m_cnt(0){}string First(){return m_aggregate->POp(0);}string Next(){string str;m_cnt++;if (m_cnt < m_aggregate->Count()){str = m_aggregate->POp(m_cnt);}return str;}string GetCur(){return m_aggregate->POp(m_cnt);}bool IsEnd(){return m_cnt >= m_aggregate->Count() ? true : false;}~ConcreteIterator(){ }private:Aggregate *m_aggregate;int m_cnt;};class ConcreteAggregate : public Aggregate{public:ConcreteAggregate(){m_vecItem.clear();}~ConcreteAggregate(){}int Count(){return m_vecItem.size();}void Push(const string &strValue){m_vecItem.push_back(strValue);}string POp(const int cnt){string str;if (cnt < Count()){str = m_vecItem[cnt];}return str;}private:vector<string> m_vecItem;};int main(){ConcreteAggregate* pName = NULL;pName = new ConcreteAggregate();if (NULL != pName){pName->Push("hello");pName->Push("word");pName->Push("cxue");}Iterator* iter = new ConcreteIterator(pName);if (NULL != iter){string strItem = iter->First();while (!iter->IsEnd()){cout << iter->GetCur() << " is ok" << endl;iter->Next();}}delete iter;iter = NULL;system("pause");return 0;}
0 0
- 设计模式 -- Iterator(迭代器)
- Iterator(迭代器)设计模式
- Iterator 迭代器设计模式
- Iterator 迭代器设计模式
- 设计模式----Iterator(迭代器)模式
- 设计模式--迭代器模式(Iterator)
- [设计模式]Iterator迭代器模式
- 设计模式 迭代器模式 Iterator
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
- 设计模式(C#)之迭代器模式(Iterator Pattern)
- 设计模式(C#)之迭代器模式(Iterator Pattern)
- 设计模式--Iterator模式
- 设计模式---Iterator模式
- 设计模式--Iterator 模式
- 【设计模式】Iterator模式
- 设计模式之Iterator (迭代器)
- 设计模式之:Iterator迭代器
- 设计模式之Iterator迭代器
- 做了个戚风蛋糕
- POJ 2311 Cutting Game
- 3个线程顺序打印ABC10次
- awakeFromNib与viewDidLoad的区别
- 《幸运大转盘》代码分享
- c++设计模式----迭代器模式iterator
- AndroidStudio中使用NDK
- bfs实现二叉树按行有序输出
- Dijkstra算法
- 链表反转
- Unity Shader编程(1)漫反射着色
- hdoj1798Tell me the area【求两圆相交部分的面积】
- Eclipse中配置Servlet流程
- 11、vim的基本使用和技巧