Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet203/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   199   200   201   202   203   204   205   206   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
300 
PrintEmployees(*iterator); 
6.
An internal ListIterator.
As a final example, let's look at a possible 
implementation of aninternal or passive ListIterator class. Here the 
iteratorcontrols the iteration, and it applies an operation to each element. 
The issue in this case is how to parameterize the iterator with theoperation 
we want to perform on each element. C++ does not supportanonymous functions 
or closures that other languages provide for thistask. There are at least 
two options: (1) Pass in a pointer to afunction (global or static), or (2) 
rely on subclassing. In the firstcase, the iterator calls the operation 
passed to it at each point inthe iteration. In the second case, the iterator 
calls an operationthat a subclass overrides to enact specific behavior. 
Neither option is perfect. Often you want to accumulate state duringthe 
iteration, and functions aren't well-suited to that; we would haveto use 
static variables to remember the state. AnIterator subclass provides us 
with a convenient place tostore the accumulated state, like in an instance 
variable. Butcreating a subclass for every different traversal is more work. 
Here's a sketch of the second option, which uses subclassing. We callthe 
internal iterator a ListTraverser. 
template  
class ListTraverser { 
public: 
ListTraverser(List* aList); 
bool Traverse(); 
protected: 
virtual bool ProcessItem(const Item&) = 0; 
private: 
ListIterator _iterator; 
}; 
ListTraverser takes a List instance as a parameter.Internally it uses an 
external ListIterator to do thetraversal. Traverse starts the traversal 
and callsProcessItem for each item. The internal iterator can choose 
toterminate a traversal by returning false fromProcessItem. Traverse 
returns whether the traversalterminated prematurely. 
template  
ListTraverser::ListTraverser ( List* aList )
: _iterator(aList) { } 
template  



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   199   200   201   202   203   204   205   206   ...   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