computer science 209 the factory pattern. collections and iterators list list1 = new arraylist ();...
TRANSCRIPT
Computer Science 209
The Factory Pattern
Collections and Iterators List<String> list1 = new ArrayList<String>();
List<String> list2 = new LinkedList<String>();
Set<String> set1 = new HashSet<String>();
SortedSet<String> set2 = new TreeSet<String>();
// Add some stuff to these collections
Iterator<String> iter1 = list1.iterator();
Iterator<String> iter2 = list2.iterator();
Iterator<String> iter3 = set1.iterator();
Iterator<String> iter4 = set2.iterator();
Each type of collection manufactures its own iterator
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
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
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
The Factory Setup
<<Interface>>Creator
ConcreteCreator
Client
factoryMethod()
factoryMethod()
<<Interface>>Product
ConcreteProduct
Iterating from Last to FirstList<String> aList = new ArrayList<String>();
// Add some strings to aList
ListIterator<String> 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
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
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
The Riterator Interface
public interface Riterator<E>{
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)
Iterating in Either DirectionTrueStack<String> aStack = new ArrayStack<String>();
// Add some strings to aStack
for (String item : aStack) // Move from bottom to top System.out.println(item);
Riterator<String> 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
public Iterator<E> iterator(){ return new StackIterator<E>(list.iterator());}
private class StackIterator<E> implements Iterator<E>{
private Iterator<E> iter;
private StackIterator(Iterator<E> iter){ this.iter = iter; }
// Other methods }
The Implementing Class
NestedwithinArrayStack
private class StackIterator<E> implements Iterator<E>{
private Iterator<E> 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
How would you implement the riterator for the ArrayStack class?