迭代器在STL運(yùn)用廣泛,類似容器的迭代已經(jīng)成為其重要特性,而迭代器模式則是利用迭代器概念進(jìn)行的抽象運(yùn)用,迭代器模式運(yùn)用廣泛和有用,因?yàn)槠淠軌虿豢紤]數(shù)據(jù)的存儲(chǔ)方式,而是直接面對(duì)數(shù)據(jù)進(jìn)行迭代,也就是說(shuō)我們不用考慮集合是數(shù)組(或vector)、鏈表、棧還是隊(duì)列,而是通過(guò)統(tǒng)一的接口進(jìn)行順序的訪問(wèn)。
作用
迭代器模式提供了一種順序訪問(wèn)容器中元素的方法,而無(wú)需了解器內(nèi)部的類型和結(jié)構(gòu),該模式的核心思想將訪問(wèn)和遍歷容器對(duì)象的功能交給一個(gè)外部的迭代器對(duì)象,該迭代器定義了訪問(wèn)聚合對(duì)象的接口,
類視圖
實(shí)現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
class Item { public : Item(string strName, float price): m_name(strName),m_price(price){} Item(Item& item): m_name(item.strName),m_price(item.price){} string tostring() { std::ostringstream buffer; buffer << f; string strPrice = buffer.str(); strName += " :" ; return strName + strPrice; } private : string m_name; float m_price; } class Container; class Menu; class Iterator { public : virtual ~Iterator(){} virtual void first()=0; virtual void next()=0; virtual bool hasnext()=0; virtual Item* current()=0; protected : Container * m_pContainer; } class Container { public : virtual ~Container() {}; virtual void CreateIterator()= 0; protected : Observer(){}; }; class MenuIterator : public Iterator { Menu * m_menu; int curpos; public : MenuIterator(Menu*a):m_menu(a),curpos(0){} virtual void first() { curpos=0; } virtual void next() { curpos++; } virtual bool hasnext() { if (curpos >=0 && curpos< m_menu->size()) } virtual Item* current() { return m_menu->value(curpos); } }; class Menu : public Container { public : virtual ~Menu() { for ( int i=0 ; i< m_items.size(); i++) { delete m_items[i]; } }; Iterator* CreateIterator() { return new MenuIterator( this ); } int size() { return m_items.size(); } Item* value( int nIndex) { if (nIndex>=0 && nIndex<m_items.size()) { return m_items[nIndex]; } else { return NULL; } } void additem(Item& item) { Item *pItem = new Item(item); m_items.push_back(pItem); } private : vector<item* > m_items; }; int main() { Item it1( "chicken" , 10.0); Item it2( "Apple" , 5.0); Item it3( "Beaf" , 20.0); Item it4( "soup" ,15.0); Menu menu; menu.additem(it1); menu.additem(it2); menu.additem(it3); menu.additem(it4); Iterator itor = menu.CreateIterator(); while (itor->hasnext()) { Item* pItem = itor->current(); if (pItem) cout<<pItem->tostring()<<endl; itor->next(); } } |
對(duì)于上例來(lái)說(shuō),Iterator接口是不變的,不管menu中的聚合內(nèi)容的形式如何變化,只要menu根據(jù)其類型提供其取值、大小等的操作,那么對(duì)于使用者來(lái)說(shuō)都是一樣的操作。
應(yīng)用場(chǎng)景
- 訪問(wèn)一個(gè)聚合對(duì)象的內(nèi)容而無(wú)需暴露它的內(nèi)部表示;
- 支持對(duì)聚合對(duì)象的多種遍歷(從前到后,從后到前);
- 為遍歷不同的聚合結(jié)構(gòu)提供一個(gè)統(tǒng)一的接口,支持多態(tài)迭代。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/chencarl/archive/2018/04/07/8729670.html