设计模式 - Iterator 模式(迭代器模式)
作用:提供一种方法顺序访问一个聚合对象中各个元素,,而又不需暴露该对象的内部表示。
UML结构图:
解析:
Iterator几乎是大部分人在初学C++的时候就无意之中接触到的第一种设计模式,因为在STL之中,所有的容器类都有与之相关的迭代器.以前初学STL的时候,时常在看到讲述迭代器作用的时候是这么说的:提供一种方式,使得算法和容器可以独立的变化,而且在访问容器对象的时候不必暴露容器的内部细节,具体是怎么做到这一点的呢?在STL的实现中,所有的迭代器(Iterator)都必须遵照一套规范,这套规范里面定义了几种类型的名称,比如对象的名称,指向对象的指针的名称,指向对象的引用的名称....等等,当新生成一个容器的时候与之对应的Iterator都要遵守这个规范里面所定义的名称,这样在外部看来虽然里面的实现细节不一样,但是作用(也就是对外的表象)都是一样的,通过某个名称可以得到容器包含的对象,通过某个名称可以得到容器包含的对象的指针等等的.而且,采用这个模式把访问容器的重任都交给了具体的iterator类中.于是,在使用Iterator来访问容器对象的算法不需要知道需要处理的是什么容器,只需要遵守事先约定好的Iterator的规范就可以了;而对于各个容器类而言,不管内部的事先如何,是树还是链表还是数组,只需要对外的接口也遵守Iterator的标准,这样算法(Iterator的使用者)和容器(Iterator的提供者)就能很好的进行合作,而且不必关心对方是如何事先的,简而言之,Iterator就是算法和容器之间的一座桥梁.在下面的实现中,抽象基类Iterator可以看做是前面提到的Iterator的规范,它提供了所有Iterator需要遵守的规范也就是对外的接口,而它的派生类ConcreateIterator则是ConcreateAggregate容器的迭代器,它遵照这个规范对容器进行迭代和访问操作.
代码实现:
Iterator.h
1 #ifndef ITERATOR_H
2 #define ITERATOR_H
3
4 typedef int DATA;
5
6 class Iterater;
7
8 // 容器的抽象基类
9 class Aggregate
10 {
11 public:
12 virtual ~Aggregate(){}
13
14 virtual Iterater* CreateIterater(Aggregate *pAggregate) = 0;
15 virtual int GetSize() = 0;
16 virtual DATA GetItem(int nIndex) = 0;
17 };
18
19 // 迭代器的抽象基类
20 class Iterater
21 {
22 public:
23 virtual ~Iterater(){}
24
25 virtual void First() = 0;
26 virtual void Next() = 0;
27 virtual bool IsDone() = 0;
28 virtual DATA CurrentItem() = 0;
29
30 private:
31 };
32
33 // 一个具体的容器类,这里是用数组表示
34 class ConcreateAggregate
35 : public Aggregate
36 {
37 public:
38 ConcreateAggregate(int nSize);
39 virtual ~ConcreateAggregate();
40
41 virtual Iterater* CreateIterater(Aggregate *pAggregate);
42 virtual int GetSize();
43 virtual DATA GetItem(int nIndex);
44
45 private:
46 int m_nSize;
47 DATA *m_pData;
48 };
49
50 // 访问ConcreateAggregate容器类的迭代器类
51 class ConcreateIterater
52 : public Iterater
53 {
54 public:
55 ConcreateIterater(Aggregate* pAggregate);
56 virtual ~ConcreateIterater(){}
57
58 virtual void First();
59 virtual void Next();
60 virtual bool IsDone();
61 virtual DATA CurrentItem();
62
63 private:
64 Aggregate *m_pConcreateAggregate;
65 int m_nIndex;
66 };
67
68 #endif
69
2 #define ITERATOR_H
3
4 typedef int DATA;
5
6 class Iterater;
7
8 // 容器的抽象基类
9 class Aggregate
10 {
11 public:
12 virtual ~Aggregate(){}
13
14 virtual Iterater* CreateIterater(Aggregate *pAggregate) = 0;
15 virtual int GetSize() = 0;
16 virtual DATA GetItem(int nIndex) = 0;
17 };
18
19 // 迭代器的抽象基类
20 class Iterater
21 {
22 public:
23 virtual ~Iterater(){}
24
25 virtual void First() = 0;
26 virtual void Next() = 0;
27 virtual bool IsDone() = 0;
28 virtual DATA CurrentItem() = 0;
29
30 private:
31 };
32
33 // 一个具体的容器类,这里是用数组表示
34 class ConcreateAggregate
35 : public Aggregate
36 {
37 public:
38 ConcreateAggregate(int nSize);
39 virtual ~ConcreateAggregate();
40
41 virtual Iterater* CreateIterater(Aggregate *pAggregate);
42 virtual int GetSize();
43 virtual DATA GetItem(int nIndex);
44
45 private:
46 int m_nSize;
47 DATA *m_pData;
48 };
49
50 // 访问ConcreateAggregate容器类的迭代器类
51 class ConcreateIterater
52 : public Iterater
53 {
54 public:
55 ConcreateIterater(Aggregate* pAggregate);
56 virtual ~ConcreateIterater(){}
57
58 virtual void First();
59 virtual void Next();
60 virtual bool IsDone();
61 virtual DATA CurrentItem();
62
63 private:
64 Aggregate *m_pConcreateAggregate;
65 int m_nIndex;
66 };
67
68 #endif
69
Iterator.cpp
1
2 #include <iostream>
3 #include "Iterator.h"
4
5 ConcreateAggregate::ConcreateAggregate(int nSize)
6 : m_nSize(nSize)
7 , m_pData(NULL)
8 {
9 m_pData = new DATA[m_nSize];
10
11 for (int i = 0; i < nSize; ++i)
12 {
13 m_pData[i] = i;
14 }
15 }
16
17 ConcreateAggregate::~ConcreateAggregate()
18 {
19 delete [] m_pData;
20 m_pData = NULL;
21 }
22
23 Iterater* ConcreateAggregate::CreateIterater(Aggregate *pAggregate)
24 {
25 return new ConcreateIterater(this);
26 }
27
28 int ConcreateAggregate::GetSize()
29 {
30 return m_nSize;
31 }
32
33 DATA ConcreateAggregate::GetItem(int nIndex)
34 {
35 if (nIndex < m_nSize)
36 {
37 return m_pData[nIndex];
38 }
39 else
40 {
41 return -1;
42 }
43 }
44
45 ConcreateIterater::ConcreateIterater(Aggregate* pAggregate)
46 : m_pConcreateAggregate(pAggregate)
47 , m_nIndex(0)
48 {
49
50 }
51
52 void ConcreateIterater::First()
53 {
54 m_nIndex = 0;
55 }
56
57 void ConcreateIterater::Next()
58 {
59 if (m_nIndex < m_pConcreateAggregate->GetSize())
60 {
61 ++m_nIndex;
62 }
63 }
64
65 bool ConcreateIterater::IsDone()
66 {
67 return m_nIndex == m_pConcreateAggregate->GetSize();
68 }
69
70 DATA ConcreateIterater::CurrentItem()
71 {
72 return m_pConcreateAggregate->GetItem(m_nIndex);
73 }
74
75
2 #include <iostream>
3 #include "Iterator.h"
4
5 ConcreateAggregate::ConcreateAggregate(int nSize)
6 : m_nSize(nSize)
7 , m_pData(NULL)
8 {
9 m_pData = new DATA[m_nSize];
10
11 for (int i = 0; i < nSize; ++i)
12 {
13 m_pData[i] = i;
14 }
15 }
16
17 ConcreateAggregate::~ConcreateAggregate()
18 {
19 delete [] m_pData;
20 m_pData = NULL;
21 }
22
23 Iterater* ConcreateAggregate::CreateIterater(Aggregate *pAggregate)
24 {
25 return new ConcreateIterater(this);
26 }
27
28 int ConcreateAggregate::GetSize()
29 {
30 return m_nSize;
31 }
32
33 DATA ConcreateAggregate::GetItem(int nIndex)
34 {
35 if (nIndex < m_nSize)
36 {
37 return m_pData[nIndex];
38 }
39 else
40 {
41 return -1;
42 }
43 }
44
45 ConcreateIterater::ConcreateIterater(Aggregate* pAggregate)
46 : m_pConcreateAggregate(pAggregate)
47 , m_nIndex(0)
48 {
49
50 }
51
52 void ConcreateIterater::First()
53 {
54 m_nIndex = 0;
55 }
56
57 void ConcreateIterater::Next()
58 {
59 if (m_nIndex < m_pConcreateAggregate->GetSize())
60 {
61 ++m_nIndex;
62 }
63 }
64
65 bool ConcreateIterater::IsDone()
66 {
67 return m_nIndex == m_pConcreateAggregate->GetSize();
68 }
69
70 DATA ConcreateIterater::CurrentItem()
71 {
72 return m_pConcreateAggregate->GetItem(m_nIndex);
73 }
74
75
Main.cpp
1 #include "Iterator.h"
2 #include <iostream>
3
4 int main()
5 {
6 Aggregate* pAggregate = new ConcreateAggregate(4);
7 Iterater* pIterater = new ConcreateIterater(pAggregate);
8
9 for (; false == pIterater->IsDone(); pIterater->Next())
10 {
11 std::cout << pIterater->CurrentItem() << std::endl;
12 }
13
14 return 0;
15 }
2 #include <iostream>
3
4 int main()
5 {
6 Aggregate* pAggregate = new ConcreateAggregate(4);
7 Iterater* pIterater = new ConcreateIterater(pAggregate);
8
9 for (; false == pIterater->IsDone(); pIterater->Next())
10 {
11 std::cout << pIterater->CurrentItem() << std::endl;
12 }
13
14 return 0;
15 }