迭代器模式

来源:互联网 发布:淘宝小号点数是什么 编辑:程序博客网 时间:2024/06/07 03:03

C++实现迭代器模式

说实话这个迭代器模式实现起来有点模糊

 

[cpp] view plain copy
 
 print?
  1. /* 
  2.     迭代器模式:提供一种方法顺序访问一个聚合对象中个各个元素,而不暴露该对像的内部表示. 
  3.       模式的动机: 
  4.       (1)一个聚合对象,如一个列表(List)或者一个集合(Set),应该提供一种方法来让别人可以访问 
  5.       它的元素,而又不需要暴露它的内部结构。 
  6.       (2)针对不同的需要,可能还要以不同的方式遍历整个聚合对象,但是我们并不希望在聚合对象的 
  7.       抽象层接口中充斥着各种不同遍历的操作。 
  8.       (3)怎样遍历一个聚合对象,又不需要了解聚合对象的内部结构,还能够提供多种不同的遍历方式, 
  9.       这就是迭代器模式所要解决的问题。 
  10.  
  11.       Created by Phoenix_FuliMa 
  12. */  
  13.   
  14. #include <iostream>  
  15. #include <vector>  
  16. using namespace std;  
  17.   
  18. /* object可以是任意类型的变量 */  
  19. typedef int object;  
  20.   
  21. class Iterator  
  22. {  
  23. public:  
  24.     virtual object begin() = 0;  
  25.     virtual void   next() = 0;  
  26.     virtual object end() = 0;  
  27.     virtual object current() = 0;  
  28.     virtual bool   IsDone() = 0;  
  29. };  
  30.   
  31. class ConcreteAggregate  
  32. {  
  33. private:  
  34.     vector<object> _objects;  
  35.   
  36. public:  
  37.     void AddObject(object obj)  
  38.     {  
  39.         _objects.push_back(obj);  
  40.     }  
  41.       
  42.     object& operator[](int index)  
  43.     {  
  44.         return _objects[index];  
  45.     }  
  46.     int size()  
  47.     {  
  48.         return _objects.size();  
  49.     }  
  50. };  
  51.   
  52. class ConcreteIterator:public Iterator  
  53. {  
  54. public:  
  55.     ConcreteAggregate *agg;   
  56.     int _index;  
  57. public:  
  58.     ConcreteIterator(ConcreteAggregate *agg)  
  59.     {  
  60.         this->agg = agg;  
  61.         _index = 0;  
  62.     }  
  63.     virtual object begin()  
  64.     {  
  65.         return (*agg)[0];  
  66.     }  
  67.     virtual void next()  
  68.     {  
  69.         _index++;  
  70.     }  
  71.   
  72.     virtual object end()  
  73.     {  
  74.         _index = agg->size();  
  75.         return (*agg)[_index];  
  76.     }  
  77.   
  78.     virtual object current()  
  79.     {  
  80.         return (*agg)[_index];  
  81.     }  
  82.   
  83.     virtual bool IsDone()  
  84.     {  
  85.         return (_index == agg->size());  
  86.     }  
  87. };  
  88.   
  89. int main()  
  90. {  
  91.     ConcreteAggregate *objects =new ConcreteAggregate();  
  92.     object a = 1;  
  93.     object b = 2;     
  94.     object c = 3;  
  95.   
  96.     objects->AddObject(a);  
  97.     objects->AddObject(b);  
  98.     objects->AddObject(c);  
  99.   
  100.     ConcreteIterator *iter = new ConcreteIterator(objects);  
  101.       
  102.     object tmp_begin = iter->begin();  
  103.     while(!iter->IsDone())  
  104.     {  
  105.         cout<<iter->current()<<" ";  
  106.         iter->next();  
  107.     }  
  108.     cout<<endl;  
  109.   
  110.     delete objects;  
  111.     delete iter;  
  112.   
  113.     system("pause");  
  114.     return 0;  

原创粉丝点击