Presentation is loading. Please wait.

Presentation is loading. Please wait.

Feb 200692.3913 Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.

Similar presentations


Presentation on theme: "Feb 200692.3913 Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate."— Presentation transcript:

1 Feb 200692.3913 Ron McFadyen1 Iterator Pattern Recall 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()

2 Feb 200692.3913 Ron McFadyen2 Text Example Recall 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()

3 Feb 200692.3913 Ron McFadyen3 Sample Behaviour :DinerMenu :Waitress :DinerMenuIterator createIterator() new() hasNext() next() Loop [hasNext true] hasNext() Recall

4 Feb 200692.3913 Ron McFadyen4 Iterator Pattern Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The Collection interface defines a method: Iterator iterator() that returns an iterator Each subclass of Collection (LinkedList, Vector, …) implements iterator() differently. This allows the programmer to iterate through the objects of the collection without knowing what subclasses the iterator or collection objects belongs to. A statement such as Iterator iter = myCollection.iterator(); returns an iterator regardless of what type of collection object myCollection is. We know the iterator has certain behaviour but we don’t know what subclass the iterator belongs to Note: we cover the Iterator design pattern in a later chapter

5 Feb 200692.3913 Ron McFadyen5 Iterator Pattern The Collection interface defines a method: Iterator iterator() that returns an iterator A statement such as Iterator iter = myCollection.iterator(); returns an iterator regardless of what type of collection object myCollection is. Without a framework like this, to instantiate an iterator we would need to know the iterator class and we would have to do : Iterator iter = new MyCollectionIterator?(myCollection); i.e. we need to know the class the iterator belongs to. Using the Factory Method, we don’t know the subclass for the iterator (and we don’t need to know)

6 Feb 200692.3913 Ron McFadyen6 Iterator Pattern AbstractCollection iterator() methodA() methodB() … AbstractIterator next() hasNext() remove() Vector iterator() … List iterator() … VectorIterator next() hasNext() remove() Client … iterator() is the factory method that returns an iterator object instantiated from the correct iterator subclass for the collection subclass ListIterator next() hasNext() remove() … If we extend the capability to handle a new collection we need to provide a collection subclass and an iterator subclass

7 Feb 200692.3913 Ron McFadyen7 Iterator Pattern as an Example of Factory Method AbstractCollection iterator() methodA() methodB() … AbstractIterator next() hasNext() remove() Vector iterator() … List iterator() … VectorIterator next() hasNext() remove() Client … ListIterator next() hasNext() remove() … FactoryMethod Abstract factory Concrete factory Concrete product Abstract product


Download ppt "Feb 200692.3913 Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate."

Similar presentations


Ads by Google