Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface gives us a common way to create an iterator. The iterator interface gives us a common way to access components of the aggregate > Aggregate createIterator() next() hasNext() remove() ConcreteAggregate Client ConcreteIterator() > Iterator next() hasNext() remove() createIterator()
Feb Ron McFadyen2 Text Example UML class diagram Each menu class has responsibility to create the necessary iterator. Note that the iterator pattern is an example of a Factory (i.e. objects are created). > Menu createIterator() next() hasNext() remove() PancakeHouseMenu Waitress DinerMenuIterator() > Iterator next() hasNext() remove() createIterator() DinerMenu createIterator() PancakeHouseIterator() next() hasNext() remove()
Feb Ron McFadyen3 Sample Behaviour :DinerMenu :Waitress :DinerMenuIterator createIterator() new() hasNext() next() Loop [hasNext true] hasNext()