Presentation is loading. Please wait.

Presentation is loading. Please wait.

Feb 200692.3913 Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The.

Similar presentations


Presentation on theme: "Feb 200692.3913 Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The."— Presentation transcript:

1 Feb 200692.3913 Ron McFadyen1 Factory Method 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

2 Feb 200692.3913 Ron McFadyen2 Factory Method 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)

3 Feb 200692.3913 Ron McFadyen3 Iterator Example 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

4 Feb 200692.3913 Ron McFadyen4 With collaboration shown 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

5 Feb 200692.3913 Ron McFadyen5 Generic Factory Method Creator factoryMethod() anOperation() Product next() hasNext() remove() ConcreteCreator factoryMethod() Client // typcially anOperation() is of the form: Public Product anOperation() Product prod = factoryMethod() Return prod ConcreteProduct()

6 Feb 200692.3913 Ron McFadyen6 Behaviour concreteCreator client concreteProduct anOperation() factoryMethod() anotherOperation()

7 Feb 200692.3913 Ron McFadyen7 Behaviour Exercise: show the behaviour for when the “dynamic” pizzaTestDrive orders a Chicago style cheese pizza from the time just after the store is instantiated until just after it obtains the name of the pizza. Exercise: show the behaviour for obtaining an iterator for a linked list up to and including getting the second object in the linked list.


Download ppt "Feb 200692.3913 Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The."

Similar presentations


Ads by Google