Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.

Similar presentations


Presentation on theme: "Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet."— Presentation transcript:

1 Computer Science 209 The Factory Pattern

2 Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet (); SortedSet set2 = new TreeSet (); // Add some stuff to these collections Iterator iter1 = list1.iterator(); Iterator iter2 = list2.iterator(); Iterator iter3 = set1.iterator(); Iterator iter4 = set2.iterator(); Each type of collection manufactures its own iterator

3 Collections and Iterators The List, Set, and SortedSet interfaces all extend the Collection interface Collection includes the iterator method, which returns an object that implements the Iterator interface The various concrete collection classes are responsible for implementing concrete iterator classes and the iterator method

4 The Context of the Factory Pattern A type (the creator) needs to create objects of another type (the product) Subclasses of the creator type must create different kinds of product objects Clients don ’ t need to know the exact type of the product objects

5 Solution of the Factory Pattern Define a creator type that captures the commonality of all creators Define a product type that captures the commonality of all products Define a factory method in the creator type that returns a product object Each creator class implements the factory method so that it returns an object of its product class

6 The Factory Setup > Creator Concrete Creator Client factoryMethod() > Product Concrete Product

7 Iterating from Last to First List aList = new ArrayList (); // Add some strings to aList ListIterator iter = aList.listIterator() while (iter.hasNext()) // Move from first to last System.out.println(iter.next()); while (iter.hasPrevious()) // Move from last to first System.out.println(iter.previous()); Also includes the methods remove, add, and set, which mutate the backing store

8 Problem: Iterating Through a Stack The for loop should visit items from the bottom of a stack to its top (supports cloning a stack with the constructor) But some clients might want to visit items from the top of a stack to its bottom The same situation applies to other linear structures, such as queues

9 Solution: Implement Two Iterators The first iterator is the standard one, which supports the use of a for loop The second iterator supports visiting items in the opposite direction Its interface, called Riterator, includes the methods hasPrevious, previous, and remove The implementing class must include a factory method named riterator

10 The Riterator Interface public interface Riterator { public boolean hasPrevious() public E previous() public void remove() } remove deletes the object most recently accessed with previous remove must be included in the implementing class, but need not be supported (can throw an UnsupportedOperationException )

11 Iterating in Either Direction TrueStack aStack = new ArrayStack (); // Add some strings to aStack for (String item : aStack) // Move from bottom to top System.out.println(item); Riterator riter = aStack.riterator() while (riter.hasPrevious()) // Move from top to bottom System.out.println(riter.previous()); The factory methods are iterator and riterator The for loop implicitly uses iterator

12 public Iterator iterator(){ return new StackIterator (list.iterator()); } private class StackIterator implements Iterator { private Iterator iter; private StackIterator(Iterator iter){ this.iter = iter; } // Other methods } The Implementing Class Nested within ArrayStack

13 private class StackIterator implements Iterator { private Iterator iter; public boolean hasNext(){ return iter.hasNext(); } public E next(){ return iter.next(); } public void remove(){ throw new UnsupportedOperationException( "remove not supported by Stack"); } Other Methods

14 How would you implement the riterator for the ArrayStack class?


Download ppt "Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet."

Similar presentations


Ads by Google