Design Patterns : Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet197/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   193   194   195   196   197   198   199   200   ...   288
Bog'liq
GOF Design Patterns

externaliterator
, and when the iterator controls it, the iterator is 
an
internal iterator
.
2
Clients that use anexternal iterator must advance the 
traversal and request the nextelement explicitly from the iterator. In 


Design Patterns: Elements of Reusable Object-Oriented Software 
293 
contrast, the client handsan internal iterator an operation to perform, 
and the iterator appliesthat operation to every element in the aggregate. 
External iterators are more flexible than internal iterators. It'seasy to 
compare two collections for equality with an externaliterator, for example, 
but it's practically impossible with internaliterators. Internal iterators 
are especially weak in a language likeC++ that does not provide anonymous 
functions, closures, orcontinuations like Smalltalk and CLOS. But on the 
other hand,internal iterators are easier to use, because they define the 
iterationlogic for you. 
2.
Who defines the traversal algorithm?
The iterator is not the only place where 
the traversal algorithm canbe defined. The aggregate might define the 
traversal algorithm anduse the iterator to store just the state of the 
iteration. We callthis kind of iterator a 
cursor
, since it merely points 
tothe current position in the aggregate. A client will invoke the 
Nextoperation on the aggregate with the cursor as an argument, and theNext 
operation will change the state of thecursor.
3
If the iterator is responsible for the traversal algorithm, then it'seasy 
to use different iteration algorithms on the same aggregate, andit can also 
be easier to reuse the same algorithm on differentaggregates. On the other 
hand, the traversal algorithm might need toaccess the private variables 
of the aggregate. If so, putting thetraversal algorithm in the iterator 
violates the encapsulation of theaggregate. 
3.
How robust is the iterator?
It can be dangerous to modify an aggregate while 
you're traversing it.If elements are added or deleted from the aggregate, 
you might end upaccessing an element twice or missing it completely. A 
simplesolution is to copy the aggregate and traverse the copy, but that'stoo 
expensive to do in general. 

robust iterator
ensures that insertions and removalswon't interfere with 
traversal, and it does it without copying theaggregate. There are many ways 
to implement robust iterators. Mostrely on registering the iterator with 
the aggregate. On insertion orremoval, the aggregate either adjusts the 
internal state of iteratorsit has produced, or it maintains information 
internally to ensureproper traversal. 
Kofler provides a good discussion of how robust iterators areimplemented 
in ET++ [Kof93]. Murray discusses theimplementation of robust iterators 
for the USL StandardComponents'List class [Mur93]. 
4.
Additional Iterator operations.
The minimal interface to Iterator consists 
of the operations First,Next, IsDone, and CurrentItem.
4
Someadditional 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   193   194   195   196   197   198   199   200   ...   288




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish