016 --- 第20章 迭代器模式
简述:
迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。
迭代器模式包括:抽象聚集类、具体聚集类、抽象迭代类、具体迭代类。
抽象聚集类:封装一个抽象迭代类对象。
具体聚集类:继承自抽象聚集类。
抽象迭代类:用于定义得到开始对象、得到下一个对象、判断是否到结尾、当前对象等抽象方法,统一接口。
具体迭代类:继承自抽象迭代类,实现开始、下一个、是否结尾、当前对象等方法。
应用场景:类似于迭代器,当需要实现多种遍历方式时,可以使用迭代器模式
注:开发环境调整为VS2017,操作系统win11
迭代器模式:
1 #include <iostream> 2 #include <string> 3 #include <list> 4 using namespace std; 5 6 class CObject 7 { 8 public: 9 string m_szData; 10 11 public: 12 CObject(string szData) : m_szData(szData){} 13 }; 14 15 // 抽象迭代器类 16 class CIterator 17 { 18 public: 19 virtual CObject* First() = 0; 20 virtual CObject* Next() = 0; 21 virtual bool IsDone() = 0; 22 virtual CObject* CurrentItem() = 0; 23 }; 24 25 // 抽象聚集类 26 class CAggregate 27 { 28 public: 29 virtual CIterator* CreateIterator(CIterator* pIterator) = 0; 30 }; 31 32 // 具体聚集类 33 class CConcreteAggregate : public CAggregate 34 { 35 private: 36 list<CObject*> m_lstItems; 37 CIterator* m_pIterator; 38 39 public: 40 virtual CIterator* CreateIterator(CIterator* pIterator) 41 { 42 m_pIterator = pIterator; 43 return m_pIterator; 44 } 45 46 int Count() { return m_lstItems.size(); } 47 48 CObject* operator[](int nIndex) 49 { 50 CObject* pObj = NULL; 51 if (nIndex > m_lstItems.size() - 1) 52 return pObj; 53 54 list<CObject*>::iterator ite = m_lstItems.begin(); 55 int n = 0; 56 while (ite != m_lstItems.end()) 57 { 58 if (n == nIndex) 59 { 60 pObj = (*ite); 61 break; 62 } 63 64 n++; 65 ite++; 66 } 67 68 return pObj; 69 } 70 71 list<CObject*>& GetList() { return m_lstItems; } 72 }; 73 74 // 具体迭代类 75 class CConcreteIterator : public CIterator 76 { 77 private: 78 CConcreteAggregate* m_pAggregate; 79 int m_nCurrent; 80 81 public: 82 CConcreteIterator(CConcreteAggregate* pAggregate) 83 { 84 m_pAggregate = pAggregate; 85 m_nCurrent = 0; 86 } 87 88 virtual CObject* First() { return (*m_pAggregate)[0]; } 89 90 virtual CObject* Next() 91 { 92 CObject* pObject = NULL; 93 m_nCurrent++; 94 if (m_nCurrent < m_pAggregate->Count()) 95 pObject = (*m_pAggregate)[m_nCurrent]; 96 97 return pObject; 98 } 99 100 virtual bool IsDone() { return m_nCurrent >= m_pAggregate->Count() ? true : false; } 101 102 virtual CObject* CurrentItem() { return (*m_pAggregate)[m_nCurrent]; } 103 }; 104 105 // 具体反向迭代类 106 class CConcreteIteratorDesc : CIterator 107 { 108 private: 109 CConcreteAggregate* m_pAggregate; 110 int m_nCurrent; 111 112 public: 113 CConcreteIteratorDesc(CConcreteAggregate* pAggregate) 114 { 115 m_pAggregate = pAggregate; 116 m_nCurrent = m_pAggregate->Count() - 1; 117 } 118 119 virtual CObject* First() { return (*m_pAggregate)[m_pAggregate->Count() - 1]; } 120 121 virtual CObject* Next() 122 { 123 CObject* pObject = NULL; 124 m_nCurrent--; 125 if (m_nCurrent < m_pAggregate->Count()) 126 pObject = (*m_pAggregate)[m_nCurrent]; 127 128 return pObject; 129 } 130 131 virtual bool IsDone() { return m_nCurrent < 0 ? true : false; } 132 133 virtual CObject* CurrentItem() { return (*m_pAggregate)[m_nCurrent]; } 134 }; 135 136 int main() 137 { 138 CConcreteAggregate Aggregate; 139 CObject obj("GGG"), obj1("HHH"), obj2("LLL"); 140 Aggregate.GetList().push_back(&obj); 141 Aggregate.GetList().push_back(&obj1); 142 Aggregate.GetList().push_back(&obj2); 143 144 //CConcreteIterator Iterator(&Aggregate); 145 CConcreteIteratorDesc Iterator(&Aggregate); 146 CObject* pObj = Iterator.First(); 147 while (!Iterator.IsDone()) 148 { 149 cout << Iterator.CurrentItem()->m_szData << " 请买车票!" << endl; 150 Iterator.Next(); 151 } 152 153 system("pause"); 154 return 0; 155 }
输出结果: