Copyright © 2002, Systems and Computer Engineering, Carleton University. 94.204-14-Patterns.ppt 1 94.204* Object-Oriented Software Development Part 11.

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
15-Jun-15 Lists in Java Part of the Collections Framework.
1 Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Ralph Johnson - University of Illinois1 Patterns: What They Are, and How to Write Them Ralph Johnson University of Illinois at Urbana-Champaign
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Iterator Pattern.
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.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Object-oriented design Part 4: More UML. Interfaces An interface is a language construct specific to Java Java does not support multiple inheritance Interfaces.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Introduction to Design Patterns 1CS Computer Science II.
Introduction to Design Patterns 1CS Computer Science II.
Chapter 9: The Iterator Pattern
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
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.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
Copyright © 2002, Systems and Computer Engineering, Carleton University EventModel.ppt * Object-Oriented Software Development Part 17.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Patterns Composite Pattern. Patterns All designers use patterns. Patterns in solutions come from patterns in problems. "A pattern is a solution to a problem.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Introduction to Design Patterns 1CS Computer Science II.
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.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
The Object-Oriented Thought Process Chapter 15
Design Patterns Introduction
08/15/09 Design Patterns James Brucker.
Collections Framework
Introduction to Design Patterns
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Software Design Lecture : 39.
Part of the Collections Framework
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Presentation transcript:

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11 Introduction to Design Patterns: Iterators revised March 6, 2002

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 2 Iterating Through A Vector Suppose we want to "step through" a Vector object, retrieving each of the objects stored in the vector, one at a time; for example: import java.util.*; public class IterationExample1 { public static void main(String args[]) { Vector v = new Vector(); // Put some Complex number // objects in the vector. v.addElement(new Complex(3.2, 1.7));

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 3 Iterating Through A Vector v.addElement(new Complex(7.6)); v.addElement(new Complex()); v.addElement(new Complex(-4.9, 8.3)); Complex c; for (int i = 0; i < v.size(); i++) { c = (Complex)v.elementAt(i); System.out.println(c); }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 4 Iterating Through A Vector This approach works well for vectors because the elements of a vector can be retrieved by specifying their position: v.elementAt(i); We start by retrieving element 0, then retrieve element 1, etc., until we retrieve the last element (at position v.size() - 1)

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 5 What About Other Collection Classes? This approach doesn't work well for all collection classes: –what would a position (i.e. index) mean for a Set ? –values stored in a Hashtable object are retrieved by keys, not by position Is it possible to come up with a more general approach, that can be used to iterate over any collection of objects? There must be a general design solution to this…

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 6 Design Patterns "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice." Christopher Alexander, A Pattern Language: Towns/Buildings/Construction, 1977 A object-oriented design pattern systematically names, explains and evaluates an important and recurring design in object-oriented systems

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 7 Design Patterns The landmark book on software design patterns is: Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Addison-Wesley, 1995 This is also known as the GOF (“Gang-of-Four”) book.

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 8 Design Patterns Design Patterns follow a certain structure: Name Problem Solution Forces (or Consequences) Additional topics: Implementation Known uses Related Patterns

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 9 Design Pattern Problem: how can I “provide a way to access the elements of an aggregate object (i.e., an object that contains a collection of objects: Vector, Hashtable, LinkedList,...) sequentially without exposing its underlying representation”?

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 10 Iterator Pattern Solution: The responsibility for accessing and traversing an aggregation object is placed in a separate iterator object, not in the aggregation object All iterator objects should provide the same interface for accessing and traversing elements, regardless of the class of the aggregate object and the kind of traversal that is performed

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 11 GOF Iterator Pattern Iterator next() isDone() … ConcreteIterator ConcreteAggregate createIterator() return new ConcreteIterator(this) The arrow represents the association between ConcreteIterator and ConcreteAggregate : a ConcreteIterator object holds a reference to the ConcreteAggregate object that created it. createIterator() Aggregate

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 12 GOF Iterator Pattern Participants: Iterator –defines an interface for accessing and traversing elements ConcreteIterator –implements the Iterator interface. –keeps track of the current position in the traversal of the aggregate Aggregate –defines an interface for creating an Iterator object ConcreteAggregate –creates and returns an instance of the proper ConcreteIterator

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 13 GOF Iterator Pattern Forces / Consequences: “It supports variations in the traversal of an aggregate”: you can use different ways of iterating by defining various concrete subclasses of Iterator for your collection. “Iterators simplify the Aggregate interface” “More than one traversal can be pending on an Aggregate”

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 14 GOF Iterator Pattern Implementation: Good news: there is direct support for the iterator pattern in Java! In the previous slides, the names of the participating classes and the operations they provide were the ones used in the GOF book. As we shall see, the support for the iterator pattern provided in the Java SDK is based on Java interfaces, not abstract classes (why?), and the operations differ slightly from the ones shown in the diagram, but the basic idea is the same.

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 15 Collection Classes & Iterator Objects The Collection interface provides an iterator() method to be implemented by all concrete collections: Iterator iterator() –Collection plays the role of the “Aggregate” in the GoF Iterator pattern A “ConcreteAggregate” (i.e. Vector, LinkedList …) implementing this method should create and return a iterator object

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 16 Iterator Objects An Iterator object mainly provides two methods: –public boolean hasNext() –returns true if the Iterator object has more elements (objects) that have not yet been returned by next() –public Object next() –returns the next element (object) from the iteration

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 17 Iterating Through a Vector // Create a vector object and initialize // it with Complex objects Complex c; Iterator i; i = v.iterator(); while (i.hasNext()) { c = (Complex)i.next(); System.out.println(c); }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 18 Iterating Through Any Collection ex: the code below shows how AbstractCollection implements addAll() without making any assumption on the nature of the collection that is passed as an argument: public boolean addAll(Collection from) { Iterator iter = from.iterator(); boolean modified = false; while (iter.hasNext()) if (add(iter.next())) modified = true; return modified }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 19 Iterating Through a Hashtable Hashtable does not implement Collection, and therefore does not have methods that return Iterator objects for the keys and values in the table Instead: –entrySet() returns a Set of the entries in the table –keySet() returns a Set of the keys in the table –values() returns a Collection of the values in the table iterator() messages are sent to these objects See the Java 1.2 API Specification for more information

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 20 Enumerations The “old” way (before the introduction of the Collection Framework) of stepping through a Vector or a Hashtable was to use Enumeration objects. Enumerations are similar to iterators and provide the two following methods: boolean hasMoreElements() Object next() To get an Enumeration for a Vector, call the elements() method on the Vector object. To get an Enumeration for the keys of a Hashtable, use the keys() method on the Hashtable object. To get an Enumeration for the elements of a Hashtable, use the elements() method on the Hashtable object.

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 21 Building an Iterator Suppose we have a class called CircularList that provides these methods (other CircularList methods not shown here): // Constructor. public CircularList(); // Return the count of the number of // elements in the list. public int count(); // Return the element stored at the specified // position in the CircularList. public Object get(int index); // Remove the element stored at the specified // position in the CircularList. public Object remove(int index);

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 22 Building an Iterator How do we build an iterator (i.e., an Iterator object) for this class? We want an object that lets us do this: CircularList l = new CircularList();... //store integers in the list... Iterator i = l.iterator(); while (i.hasNext()) { System.out.println(i.next()); }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 23 The Iterator Interface When we look through the documentation for the Java API, we discover that Iterator is an interface, not a class: public interface Iterator { public boolean hasNext(); public Object next(); public void remove(); } An Iterator object is an instance of a class that implements the Iterator interface; i.e., defines the hasNext(), next(), and remove() methods

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 24 Class CircularListIterator Iterator objects for class CircularList will be instances of class CircularListIterator: class CircularListIterator implements Iterator { public boolean hasNext() {} public Object next() {} public void remove() {} }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 25 Class ListIterator How can the Iterator object (an instance of CircularListIterator ) access the elements stored in a CircularList object? –the easiest way is for the CircularListIterator object to contain a reference to the CircularList object –it can then send messages to the CircularList object to retrieve the elements

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 26 Class CircularListIterator class CircularListIterator implements Iterator { private CircularList myList; public Object next() {} public boolean hasNext() {} public void remove() {} }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 27 Class CircularListIterator How does a CircularListIterator object obtain the reference to the CircularList object? –pass a reference to the CircularList object to CircularListIterator ’s constructor

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 28 Class CircularListIterator class CircularListIterator implements Iterator { private CircularList myList; public CircularListIterator(CircularList theList) { myList = theList; } public Object next() {} public boolean hasNext() {} public void remove() {} }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 29 Class CircularListIterator How should a CircularListIterator object traverse a CircularList object? Because elements in a list are accessed by position, an easy way is to traverse the list from front to back

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 30 Class CircularListIterator class CircularListIterator implements Iterator { private CircularList myList; // constructor not shown private int pos = 0; public Object next() { Object o = myList.get(pos); // Update pos to indicate the position of // the element that will be returned the // next time this method is invoked. pos++; return o; }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 31 Class CircularListIterator public boolean hasNext() { return(pos < myList.count()); } public void remove() { if (pos > 0) { myList.remove(pos-1); // This method should throw an // IllegalStateException if next() has not // yet been invoked, or if remove() has // already been invoked after the last // invocation of next(). }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 32 Modifying Class CircularList All that remains is to define a method in CircularList to create and return a CircularListIterator object In order to be consistent with other Java collection classes, we'll call the method iterator() class CircularList { public Iterator iterator() { // Create a CircularListIterator object,passing // it a reference to this CircularList object. return new CircularListIterator(this); }

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 33 Interfaces and is-a Relationships Notice that CircularList ’s iterator() returns a reference to a CircularListIterator object, but the method’s return type is Iterator Q: Why is this o.k? A: CircularListIterator implements the Iterator interface –i.e., a CircularListIterator object is-a(n) Iterator object In other words, is-a relationships exist between –a subclass and a superclass –an interface and the class that implements the interface

Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt 34 CircularList-CircularListIterator Relationships CircularList Iterator The arrow represents the association between CircularListIterator and CircularList : a CircularListIterator object holds a reference to the CircularList object that created it. CircularListIterator implements the Iterator interface