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.