GoF Statement of Intent:![]() |
Provide a way to access the elements of an aggregate
object sequentially without exposing its underlying representation. |
Brief Overview:![]() |
Iterators let you access elements in a collection of objects with functions
like hasNext() to see if there is another object in the collection
and getNext() to get a pointer to that object. This way you have
a common way of accessing all elements without having to know how the
underlying collection is implemented, i.e. list, array, tree, etc. |
UML Diagram: |
![]() |
class Waitress { private: PancakeHouseMenu *pancakeHouseMenu; CafeMenu *cafeMenu; public: Waitress(PancakeHouseMenu *phm, CafeMenu *cm) { this->pancakeHouseMenu=phm; this->cafeMenu = cm; } void printMenus() { MenuIterator *phIterator = pancakeHouseMenu->getIterator(); Iterator *cIterator = cafeMenu->getIterator(); cout << "MENU\n----\nBREAKFAST"; printMenu(phIterator); cout << "MENU\n----\nLunch"; printMenu(cIterator); } void printMenu(Iterator *itr) { while(itr->hasNext) { MenuItem *menuItem = itr->next(); menuItem->print(); } } }