Feb 200692.3913 Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.

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

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ITEC200 Week04 Lists and the Collection Interface.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
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.
March Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
UML – Class Diagrams.
Feb Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The.
Design Patterns. What is a Design Pattern? Generic pattern for solving a certain class of problem Learn to recognize the class of problem and apply a.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Fall 2007ACS Ron McFadyen1 Composite Pattern (see pages ) A composite is a group of objects in which some objects contain others; one object.
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
Fall 2009ACS Ron McFadyen1 The context maintains an instance of a concrete state subclass State Pattern Each subclass (concrete state) implements.
Spring 2010ACS-3913 Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Useful for.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Winter 2015ACS Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 28 Iterator Summary prepared by Kirk Scott 1.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Chapter 9: The Iterator Pattern
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Chapter 18 Java Collections Framework
1/20/03A2-1 CS494 Interfaces and Collection in Java.
November Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent groups,
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
Chapter 7: The Adapter Pattern. Object Oriented Adapters Suppose that you have existing software. You have outsourced some of your work and there is a.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Computer Science 209 Software Development Inheritance and Composition.
CS 210 Iterator Pattern October 31 st, Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Session 30 Final Review. Final Details Wednesday, December 14 at 8 AM Wright 5 (same classroom) Final will be comprehensive Open book Open notes Test.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
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.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Template Method Pattern Iterator Pattern
Software Development Inheritance and Composition
Iterator Design Pattern
Iterator and Composite Design Patterns
Adapter, Fascade, and More
CSE 143 Lecture 27: Advanced List Implementation
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Menu item at a restaurant
The iterator and memento patterns
Graphical User Interfaces in Java Event-driven programming
Interator and Iterable
JCF Collection classes and interfaces
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Presentation transcript:

Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface gives us a common way to create an iterator. The iterator interface gives us a common way to access components of the aggregate > Aggregate createIterator() next() hasNext() remove() ConcreteAggregate Client ConcreteIterator() > Iterator next() hasNext() remove() createIterator()

Feb Ron McFadyen2 Text Example Recall UML class diagram Each menu class has responsibility to create the necessary iterator. Note that the iterator pattern is an example of a Factory (i.e. objects are created). > Menu createIterator() next() hasNext() remove() PancakeHouseMenu Waitress DinerMenuIterator() > Iterator next() hasNext() remove() createIterator() DinerMenu createIterator() PancakeHouseIterator() next() hasNext() remove()

Feb Ron McFadyen3 Sample Behaviour :DinerMenu :Waitress :DinerMenuIterator createIterator() new() hasNext() next() Loop [hasNext true] hasNext() Recall

Feb Ron McFadyen4 Iterator Pattern Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The Collection interface defines a method: Iterator iterator() that returns an iterator Each subclass of Collection (LinkedList, Vector, …) implements iterator() differently. This allows the programmer to iterate through the objects of the collection without knowing what subclasses the iterator or collection objects belongs to. A statement such as Iterator iter = myCollection.iterator(); returns an iterator regardless of what type of collection object myCollection is. We know the iterator has certain behaviour but we don’t know what subclass the iterator belongs to Note: we cover the Iterator design pattern in a later chapter

Feb Ron McFadyen5 Iterator Pattern The Collection interface defines a method: Iterator iterator() that returns an iterator A statement such as Iterator iter = myCollection.iterator(); returns an iterator regardless of what type of collection object myCollection is. Without a framework like this, to instantiate an iterator we would need to know the iterator class and we would have to do : Iterator iter = new MyCollectionIterator?(myCollection); i.e. we need to know the class the iterator belongs to. Using the Factory Method, we don’t know the subclass for the iterator (and we don’t need to know)

Feb Ron McFadyen6 Iterator Pattern AbstractCollection iterator() methodA() methodB() … AbstractIterator next() hasNext() remove() Vector iterator() … List iterator() … VectorIterator next() hasNext() remove() Client … iterator() is the factory method that returns an iterator object instantiated from the correct iterator subclass for the collection subclass ListIterator next() hasNext() remove() … If we extend the capability to handle a new collection we need to provide a collection subclass and an iterator subclass

Feb Ron McFadyen7 Iterator Pattern as an Example of Factory Method AbstractCollection iterator() methodA() methodB() … AbstractIterator next() hasNext() remove() Vector iterator() … List iterator() … VectorIterator next() hasNext() remove() Client … ListIterator next() hasNext() remove() … FactoryMethod Abstract factory Concrete factory Concrete product Abstract product