C Chapter16 迭代器模式

2021-05-22 20:52:21 字數 1076 閱讀 3752

迭代器模式(iterator)

提供一種方法順序訪問乙個聚合物件中各個元素,而又不暴露該物件的內部表示。當你需要訪問乙個聚集物件,而且不管這些物件是什麼都需要遍歷的時候,就應該考慮用迭代器模式。同時需要對聚集有多種方式遍歷時,可以考慮用迭代器模式。為遍歷不同的聚集結構提供如開始、下乙個、是否結束、當前哪一項等統一介面。迭代器模式就是分離了集合物件的遍歷行為,抽象出乙個迭代器類來負責,這樣既可以做到不暴露集合的內部結構,又可讓外部**透明地訪問集合內部的資料。

實際應用

工程結構:

(1)抽象迭代器 iterator.h

(2)抽象聚集類 aggregate.h

(3)具體迭代器 concreteiterator.h

(4)具體聚集類 concreteaggregate.cpp、concreteaggregate.h

(1)抽象迭代器 iterator.h

(2)抽象聚集類 aggregate.h

(3)具體迭代器 concreteiterator.h

(4)具體聚集類 concreteaggregate.cpp、concreteaggregate.h

//#include "stdafx.h"

#include "stdafx.h"

#include "concreteaggregate.h"

#include "concreteiterator.h"

#include using namespace std;

int _tmain(int argc, _tchar* argv)

}}

system("pause");

if (null != pname)

return 0;

}

設計模式(16) 迭代器 Iterator

目錄 設計模式學習筆記首頁 設計模式 1 抽象工廠 abstractfactory 設計模式 2 生成器 builder 設計模式 3 工廠方法 factory method 設計模式 4 原型 prototype 設計模式 5 單例 singleton 設計模式 6 介面卡 adapter 設計模...

23種設計模式 16 迭代器模式

迭代器介面 public inte ce iterator 資料容器介面 public inte ce idatacontainer 資料物件 public class data idatacontainer 集合長度 public int length 根據索引獲取值 public int get...

迭代器模式

迭代器模式 iterator 提供一種方法順序訪問乙個聚合物件中各個元素,而不是暴露該物件的內部表示。乙個聚集物件,而且不管這些物件是什麼都需要遍歷的時候,你就應該考慮用迭代器模式。你需要對聚集有多種方式遍歷時,可以考慮用迭代器模式。為遍歷不同的聚集結構提供如開始,下乙個,是否結束,當前哪一項等統一...