1.Iterator模式用来解决对一个聚合对象的遍历问题,将对聚合的遍历封装到一个类中进行,这样就避免了暴露这个聚合对象的内部表示的可能.
2.Iterator 模式结构图
3.实现
1 #ifndef _AGGREGATE_H_ 2 #define _AGGREGATE_H_ 3 4 class Iterator; 5 typedef int Object; 6 class Interator; 7 8 class Aggregate 9 { 10 public: 11 virtual ~Aggregate(); 12 virtual Iterator* CreateIterator() = 0; 13 virtual Object GetItem(int idx) = 0; 14 virtual int GetSize() = 0; 15 16 protected: 17 Aggregate(); 18 19 private: 20 }; 21 22 class ConcreteAggregate:public Aggregate 23 { 24 public: 25 enum 26 { 27 SIZE = 3 28 }; 29 ConcreteAggregate(); 30 ~ConcreteAggregate(); 31 Iterator* CreateIterator(); 32 Object GetItem(int idx); 33 int GetSize(); 34 35 protected: 36 private: 37 Object _objs[SIZE]; 38 }; 39 40 #endif