Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



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

Design Patterns: Elements of Reusable Object-Oriented Software 
299 

Now we're in a position to write the code for printingthe employees 
independent of a concrete representation. 
// we know only that we have an AbstractList 
AbstractList* employees
// ... 
Iterator* iterator = employees->CreateIterator(); 
PrintEmployees(*iterator); 
delete iterator
5.
Making sure iterators get deleted.
Notice that CreateIterator returns a 
newly allocatediterator object. We're responsible for deleting it. If we 
forget,then we've created a storage leak. To make life easier for 
clients,we'll provide an IteratorPtr that acts as a proxy for aniterator. 
It takes care of cleaning up the Iterator objectwhen it goes out of scope. 
IteratorPtr is always allocated on thestack.
5
C++ automatically takes care 
of callingits destructor, which deletes the real iterator.IteratorPtr 
overloads bothoperator-> andoperator* in such a way that an IteratorPtr 
can betreated just like a pointer to an iterator. The members ofIteratorPtr 
are all implemented inline; thus they can incur nooverhead. 
template  
class IteratorPtr { 
public: 
IteratorPtr(Iterator* i): _i(i) { } 
~IteratorPtr() { delete _i; } 
Iterator* operator->() { return _i; } 
Iterator& operator*() { return *_i; } 
private: 
// disallow copy and assignment to avoid 
// multiple deletions of _i: 
IteratorPtr(const IteratorPtr&); 
IteratorPtr& operator=(const IteratorPtr&); 
private: 
Iterator* _i; 
}; 
IteratorPtr lets us simplify our printing code: 
AbstractList* employees; 
// ... 
IteratorPtr iterator(employees->CreateIterator()); 



Download 4,06 Mb.

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