Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



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

Design Patterns: Elements of Reusable Object-Oriented Software 
296 
// ... 
}; 
The List class provides a reasonably efficient way tosupport iteration 
through its public interface. It's sufficient toimplement both traversals. 
So there's no need to give iteratorsprivileged access to the underlying 
data structure; that is, theiterator classes are not friends of List. To 
enabletransparent use of the different traversals we define an 
abstractIterator class, which defines the iterator interface. 
template  
class Iterator { 
public: 
virtual void First() = 0; 
virtual void Next() = 0; 
virtual bool IsDone() const = 0; 
virtual Item CurrentItem() const = 0; 
protected: 
Iterator(); 
}; 
2.
Iterator subclass implementations.
ListIterator is a subclass of Iterator. 
template  
class ListIterator : public Iterator
public: 
ListIterator(const List* aList); 
virtual void First(); 
virtual void Next(); 
virtual bool IsDone() const; 
virtual Item CurrentItem() const; 
private: 
const List* _list; 
long _current; 
}; 
The implementation of ListIterator is straightforward. Itstores the List 
along with an index _current intothe list: 
template  
ListIterator::ListIterator ( const List* aList ) 
: _list(aList), _current(0) { } 
First positions the iterator to the first element: 


Design Patterns: Elements of Reusable Object-Oriented Software 
297 
template  
void ListIterator::First () 
{ _current = 0; } 
Next advances the current element: 
template  
void ListIterator::Next ()
{ _current++; } 
IsDone checks whether the index refers to an element withinthe List: 
template  
bool ListIterator::IsDone () const
{ return _current >= _list->Count(); } 
Finally, CurrentItem returns the item at the current index.If the iteration 
has already terminated, then we throw anIteratorOutOfBounds exception: 
template  
Item ListIterator::CurrentItem () const { 
if (IsDone()) { 
throw IteratorOutOfBounds; 

return _list->Get(_current); 

The implementation of ReverseListIterator is identical, except itsFirst 
operation positions _currentto the end of the list, and Next 
decrements_current toward the first item. 
3.
Using the iterators.
Let's assume we have a List of Employee objects,and 
we would like to print all the contained employees. TheEmployee class 
supports this with a Printoperation. To print the list, we define a 
PrintEmployeesoperation that takes an iterator as an argument. It uses the 
iteratorto traverse and print the list. 
void PrintEmployees (Iterator& i) { 
for (i.First(); !i.IsDone(); i.Next()){ 
i.CurrentItem()->Print(); 


Since we have iterators for both back-to-front and front-to-backtraversals, 
we can reuse this operation to print the employees in bothorders. 



Download 4,06 Mb.

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