computer science 209 the factory pattern. collections and iterators list list1 = new arraylist ();...

14
Computer Science 209 The Factory Pattern

Upload: ezra-barber

Post on 05-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

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

Computer Science 209

The Factory Pattern

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

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

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

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

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

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

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

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

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

The Factory Setup

<<Interface>>Creator

ConcreteCreator

Client

factoryMethod()

factoryMethod()

<<Interface>>Product

ConcreteProduct

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

How would you implement the riterator for the ArrayStack class?