Iterator Matt G. Ellis. Intent Metsker: Provide a way to access elements of a collection sequentially. GoF: Provide a way to access the elements of an.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

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.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Chapter 4: Trees Part II - AVL Tree
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Advanced Data Structures
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Composite Design Pattern (1) –A structural design pattern.
Jan Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Binary Trees Binary Search Trees.  Not included in the.NET Framework  Data stored in a non-linear fashion  BST imposes rules on how the data in the.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Factory Method Joey Richey Kevin Gorski. Definition Allows a class developer define the interface for creating an object while retaining control of which.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an.
Spring 2010ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Winter 2007ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns in Java Chapter 5 Composite Summary prepared by Kirk Scott 1.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Unit 25 Composite Summary prepared by Kirk Scott 1.
Design Patterns in Java Chapter 1 Introduction Summary prepared by Kirk Scott 1.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
More on Polymorphism. Ever have one of those days?
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Composite A robot made of smaller robots.. * * Line with open diamond at the end means “aggregates”. Basically, it’s saying that the Component objects.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Chapter 4 Linked Structures.
Recursive Objects (Part 4)
Observer Design Pattern
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Summary prepared by Kirk Scott
Composite Design Pattern
Cse 373 May 15th – Iterators.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Iterator and Composite Design Patterns
Summary prepared by Kirk Scott
Presentation by Julie Betlach 7/02/2009
Jim Fawcett CSE776 – Design Patterns Summer 2003
Find in a linked list? first last 7  4  3  8 NULL
Iterator.
Object Oriented Design Patterns - Behavioral Patterns
Java Programming Arrays
CSE 1020:Inheritance Mark Shtern.
Strategy Design Pattern
CSC 143 Binary Search Trees.
Software Design Lecture : 39.
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Threads and concurrency / Safety
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Iterator Matt G. Ellis

Intent Metsker: Provide a way to access elements of a collection sequentially. GoF: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation

Motivation for Iterator Accessing each element in a collection is done often. Collections should be interchangeable Users of your collection shouldn’t have to know how it works to use it.

Type Safe Collections public List getPromotionalFireworks()  This method returns a List.  What type of objects are in the List?  Are you sure?  Would you bet your life on it?

Type Safe Collections public FireworksList getPromotinalFireworks()  This method returns a FireworksList.  What objects are in the FireworksList?  Are you sure?  Would you bet your life on it?

Type Safe Collections

Challenge 28.1 Fill in the missing code in ShowFireworkList to instantiate the iterator.

Challenge 28.1

Maintaining Type Safety May wish to inherit the underlying collection Dangerous idea, but it has its benefits How much information do you want reveal?

Iterator and Composites Composites can be thought of as a kind of collection, it may make sense to want to use an Iterator with a Composite. Due to their possibly cyclical nature, special care must be taken when writing the Iterator for a Composite (to prevent an infinite loop)

Composites at Oozinoz The Process of Building Fireworks is represented as a composite

Composites at Oozinoz

Composite Iterator Need two different types of Iterators. Iteration over a ProcessSequence is different than iteration over a ProcessStep However, we wish to provide a way of interchanging these Iterators.

Composite Iterator

CompositeIterator vs. LeafIterator The CompositeIterator class needs to create new "subiterators" as it traverses over the children of a composite object. For each child, a CompositeIterator object must create either a new leaf node iterator or a new composite iterator, depending on the type of the child. LeafIterators do not need to create "subiterators"

UML for CompositeIterator

Getting the Right Iterator The ProcessComposite class can implement iterator() to return an instance of CompositeIterator. The ProcessStep class can implement iterator() method to return an instance of LeafIterator.

Challenge 28.2 What pattern are you applying if you let classes in the ProcessComponent hierarchy implement iterator() to create instances of an appropriate iterator class?

Using the Iterator

Building the Iterator

LeafIterator

Some Notes This part is the simple one Note that the depth of an iterator is different from the depth of a node in a tree. The depth of an iterator depends on the depth of its subiterator. Leaf iterators do not have subiterators, so their depth is always 0.

CompositeIterator

Depth of a CompositeIterator is one plus the depth of its subiterator

CompositeIterator The CompositeIterator uses a peek object to facilitate the implementation of hasNext(). It can be difficult to tell whether a (possibly cyclic) composite has a next node. A simple workaround is to have hasNext() search for the next value, and report whether or not this search was successful.

CompositeIterator Check the composite bellow the current child. Move to the next child if necessary.

Challenge 28.3 We want to support a showInterior method on our Iterator. To do so we add a boolean called showInterior to ComponentIterator What changes do we have to make in CompositeIterator so that next returns only leaf nodes when showInterior is false?

Challenge 28.3

Thread Safety Modifying the Underlying collection that is being iterated over will result in odd and problematic behavior. Mutex can be used to overcome this by preventing access to a collection when an it is being access by multiple threads. Most Iterators are fail-fast in that they detect that the underlying collection has changed and raise an exception

Thread Safety When multiple threads may be accessing one data source at the same time, use java’s synchronized method to enforce Mutex. Bear in mind that doing so may effect performance of your program. Why is this the case?

Thread Safety What can we do to overcome this? Metsker’s discussion on Thread-Safe Iterators is a good one, but remember that writing multi threaded code is difficult. Understanding the problems that can arise from multiple threads running at the same time is important especially when using an Iterator.

Questions?