Design Patterns: Elements of Reusable Object-Oriented Software 301 bool ListTraverser::Traverse () {
bool result = false;
for ( _iterator.First(); !_iterator.IsDone();_iterator.Next() ) {
result = ProcessItem(_iterator.CurrentItem());
if (result == false) {
break;
}
}
return result;
}
Let's use a ListTraverser to print the first 10employees from our employee
list. To do it we have to subclassListTraverser and override ProcessItem.
Wecount the number of printed employees in a _count instancevariable.
class PrintNEmployees : public ListTraverser {
public:
PrintNEmployees(List* aList, int n) :
ListTraverser(aList),
_total(n), _count(0) { }
protected:
bool ProcessItem(Employee* const&);
private:
int _total;
int _count;
};
bool PrintNEmployees::ProcessItem (Employee* const& e) {
_count++;
e->Print();
return _count < _total;
}
Here's how PrintNEmployees prints the first 10 employeeson the list:
List* employees;
// ...
PrintNEmployees pa(employees, 10);
pa.Traverse();
Note how the client doesn't specify the iteration loop. The entireiteration
logic can be reused. This is the primary benefit of aninternal iterator.
It's a bit more work than an external iterator,though, because we have to
define a new class. Contrast this withusing an external iterator:
Design Patterns: Elements of Reusable Object-Oriented Software 302 ListIterator i(employees);
int count = 0;
for (i.First(); !i.IsDone(); i.Next()) {
count++;
i.CurrentItem()->Print();
if (count >= 10) {
break;
}
}
Internal iterators can encapsulate different kinds of iteration.
Forexample, FilteringListTraverser encapsulates aniteration that
processes only items that satisfy a test:
template class FilteringListTraverser {
public:
FilteringListTraverser(List* aList);
bool Traverse();
protected:
virtual bool ProcessItem(const Item&) = 0;
virtual bool TestItem(const Item&) = 0;
private:
ListIterator _iterator;
};
This interface is the same as ListTraverser's except for anadded TestItem
member function that defines the test.Subclasses override TestItem to
specify the test.
Traverse decides to continue the traversal based on theoutcome of the test:
template void FilteringListTraverser::Traverse () {
bool result = false;
for (_iterator.First();!_iterator.IsDone();_iterator.Next() ) {
if (TestItem(_iterator.CurrentItem())) {
result = ProcessItem(_iterator.CurrentItem());
if (result == false) {
break;
}
}
}
return result;