设计模式笔记--行为型模式之四-Iterator

意图:

提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示。

 

 

适用性:

1访问一个聚合对象的内容而无需暴露它的内部表示

2支持对聚合对象的多种遍历

3为遍历不同的聚合结构提供一个统一的接口

效果:

1它支持以不同的方式遍历一个聚合

2简化聚合的接口

3在同一个聚合上可以有多个遍历

 

实现:

1谁控制该迭代 

当客户来控制该迭代时,该迭代器称为外部迭代器。客户主动的推进遍历的步伐。

当由迭代器内部控制迭代时,该迭代器称为外部迭代器。客户只需要提交一个待执行的操作,而迭代器将对聚合中的每一个元素实施该操作

2谁定义遍历算法

聚合本身可以定义遍历算法

迭代器负责遍历算法,易于在相同的聚合上使用不同的迭代算法,也易于在不同的聚合上重用相同的算法

3迭代器的健壮性 一个健壮的迭代器保证插入和删除操作不会干扰遍历,且不需拷贝该聚合

4附加的迭代器操作 迭代器的最小接口由First,Next,IsDoneCurrentItem组成,可附加一些其他可用的操作。

5C++中使用多态的迭代器  用一个Factory Method动态的分配迭代器对象。

多态迭代器有一个缺点:客户必须负责删除它们,有一个补救方法是,适用一个栈分配的Proxy作为实际迭代器的中间代理,该代理在其析构器中删除该迭代器

 

Interator.h

 

  1. #include <iostream>
  2. using namespace std;
  3. class Employee
  4. {
  5. public:
  6.     Employee(const char* name,int age):_name(name),_age(age){}
  7.     ~Employee(){cout<<"An employee is destroyed"<<endl;}
  8.     void print(){cout<<"The employee name is "<<_name<<" and the age is "<<_age<<endl;}
  9. private:
  10.     const char* _name;
  11.     int _age;
  12. };
  13. template <class Item>
  14. class List
  15. {
  16. public:
  17.     List(long size = 0):_size(size)
  18.     {
  19.      cout<<"A list is created"<<endl;
  20.     }
  21.     long Count() const {return _size;}
  22.     
  23.     
  24.     typedef struct Node
  25.     {
  26.      
  27.         Node(){}
  28.         Item item;
  29.         Node* next;
  30.         Node* prev;
  31.     } Node;
  32.     Item Get(long index) const
  33.     {
  34.         long i =0;
  35.         Node*   p = itemlist;
  36.         for (i =0;i<index;i++)
  37.             p = p->next;
  38.         return (p->item);
  39.     }
  40.     Node* End() const
  41.     {
  42.       Node* p = itemlist;
  43.       if ((p->next == NULL)&&(p->prev == NULL))
  44.           return p;
  45.         while(p->next != NULL)
  46.         {
  47.             p= p->next;
  48.         }
  49.         return p;
  50.     }
  51.     void Append(const Item& item)
  52.     {
  53.         //Node* end = End();
  54.         Node* a = new Node;
  55.         a->item = item;
  56.         a->next = NULL;
  57.         if (_size == 0)
  58.         {
  59.             a->prev = NULL;
  60.             itemlist = a;
  61.         }
  62.         else
  63.         {
  64.         End()->next = a;
  65.         a->prev = End();
  66.         }
  67.         _size ++;
  68.     }
  69. private:
  70.     long _size;
  71.     
  72.     Node* itemlist;
  73. };
  74. template <class Item>
  75. class Iterator
  76. {
  77. public:
  78.     virtual void First() = 0;
  79.     virtual void Next() = 0;
  80.     virtual bool IsDone() const=0;
  81.     virtual Item CurrentItem() const=0;
  82. protected:
  83.     Iterator(){}
  84. };
  85. template <class Item>
  86. class ListIterator:public Iterator<Item>
  87. {
  88. public:
  89.     ListIterator(List <Item>* aList)
  90.   {
  91.     _list = aList;
  92.     _current = 0;
  93. }
  94.     virtual void First() {_current=0;}
  95.     virtual void Next() {_current++;}
  96.     virtual bool IsDone() const 
  97.     {   return _current >= _list->Count();}
  98.   virtual Item CurrentItem() const
  99.   {
  100.         if (IsDone())
  101.     {
  102.         return 0;
  103.     }
  104.     else
  105.         return _list->Get(_current);
  106.   }
  107. private:
  108.   List<Item>* _list;
  109.   long _current;
  110. };

 

main.cpp

  1. #include "Iterator.h"
  2. #include <iostream>
  3. using namespace std;
  4. void PrintEmployees(ListIterator<Employee*>& i)
  5. {
  6.     for(i.First();!i.IsDone();i.Next())
  7.         if (i.CurrentItem()!= 0)
  8.             (i.CurrentItem())->print();
  9. }
  10. int main()
  11. {
  12.     List<Employee*>* employees = new List<Employee*>;
  13.     Employee* e1= new Employee("11",11);
  14.     Employee* e2 = new Employee("22",22);
  15.     employees->Append(e1);
  16.     employees->Append(e2);
  17.     ListIterator<Employee*> forward(employees);
  18.     //ListIterator<Employee*>* forward;
  19.     //forward = new ListIterator<Employee*>(employees);
  20.     
  21.     PrintEmployees(forward);
  22.     List<int> a;
  23.     int  b =1;
  24.     int c=1;
  25.     a.Append(b);
  26.     a.Append(c);
  27.     return 0;
  28. }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值