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

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Computer Science 112 Fundamentals of Programming II List Iterators.
Double-Linked Lists and Circular Lists
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Working With Collections in the AP ™ Java Subset 2003 ACTE Convention © 2003 by Kenneth A. Lambert.
Computer Science 209 Software Development Iterators.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Feb Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
12-Jul-15 Lists in Java Part of the Collections Framework.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
COMP 103 Iterators and Iterable. RECAP  Maps and Queues TODAY  Queue Methods  Iterator and Iterable 2 RECAP-TODAY.
CS2110: SW Development Methods
(c) University of Washington14-1 CSC 143 Java Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
LinkedList Many slides from Horstmann modified by Dr V.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Java Programming: From the Ground Up Chapter 17 The Java Collections Framework.
Computer Science 209 Software Development Inheritance and Composition.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Iteration Abstraction SWE Software Construction Fall 2009.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1/20/05A-1 © 2001 T. Horton CS 494 Adv. SW Design and Development A Tasting…. Course 1: Design patterns: Intro, example Course 2: Inheritance, Interfaces,
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
University of Limerick1 Collections The Collection Framework.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 12 Iterators.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Object Oriented Programming in Java Habib Rostami Lecture 7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Iterators.
CSE 373 Implementing a Stack/Queue as a Linked List
Some Collections: BAGS, SETS, and STACKS
Software Development Iterators
Software Development Inheritance and Composition
CS240: Advanced Programming Concepts
CSE 143 Lecture 27: Advanced List Implementation
Iteration Abstraction
CSE 214 – Computer Science II Stacks
Interator and Iterable
JCF Collection classes and interfaces
Iterators Dan Fleck.
Java Generics & Iterators
Presentation transcript:

Computer Science 209 The Factory Pattern

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

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 > Creator Concrete Creator Client factoryMethod() > Product Concrete Product

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

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 { 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 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

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

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

How would you implement the riterator for the ArrayStack class?