Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.

Slides:



Advertisements
Similar presentations
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Advertisements

Behavioral Design Patterns May 5, 2015May 5, 2015May 5, 2015.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Computer Science 313 – Advanced Programming Topics.
Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-1 PS95&96-MEF-L15-1 Dr. M.E. Fayad Creationa l.
Design Patterns CS is not simply about programming
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Lexi case study (Part 2) Presentation by Matt Deckard.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
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.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
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.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Definition and Application of Binary Trees
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
classes and objects review
Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
More Design Patterns 1.
Multiuser Protection and the Mediator Pattern
More Design Patterns 1.
Jim Fawcett CSE776 – Design Patterns Summer 2003
Chapter 9 Behavioral Design Patterns
The iterator and memento patterns
Design Patterns Difficult to describe abstractly Elements:
Trees.
Software Design Lecture : 39.
CMSC 341 Binary Search Trees 2/21/2006.
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Presentation transcript:

Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral

Where is the need? Method of accessing an object’s elements without exposing internal structure. Want a compact way to keep tract of which elements have already been accessed, as well as what is the current element. Might require different traversal properties (Filtering).

How does it work?

How do I make one? Cannot simply instantiate a specific iterator class. So what do you do? Make the aggregate object responsible for the creation of its own concrete iterator. Requires operation such as CreateIterator through which clients request an iterator object.

So what can I do now? 1.Support variations in the traversals of an aggregate. (e.g. traversing a parse tree can be done inorder or preorder) 2.Makes the aggregate’s life simpler. No need to provide position calculation code into your aggregate anymore. 3.Multiple simultaneous traversals are allowed. Since the ConcreteIterator is an instance of the Abstract Iterator Class, multiple instances will each track their own progress & position.

Implementation FAQs 1. Who controls the iteration? External Iterator – When the client has control Client must advance the iterator More flexible than internal iterators for operations such as comparisons. Internal Iterator – When the iterator has control The iterator can be given an operation which it could apply to each element in the aggregate. Easier than External Iterators because iteration logic is defined for you.

Implementation FAQs 2. Who defines the traversal algorithm? Aggregate - Traversal algorithm can be part of the aggregate therefore only using the iterator as a placeholder (Cursor). Iterator - Easy to switch algorithms on the same aggregate, or reuse traversal code on other aggregates. Might violate the encapsulation of the aggregate if iterator needs access to private variables within the aggregate.

Implementation FAQs 3. How robust is the iterator? Dangerous to modify an aggregate while traversing. A robust iterator ensure that insertions and removals don’t interfere with traversal. Doesn’t need to copy the aggregate Must be registered with the aggregate so that insertions or removals will adjust the internal state of the iterator. Or, traversal information can be maintained internally by the aggregate.

Implementation FAQs 4. Any additional iterator operations? Minimum required operations: First, Next, IsDone, and CurrentItem Optional useful operations: Previous – Positions the iterator to the previous item. SkipTo – Useful for sorted or indexed collections. Can skip to an object matching certain criteria.

Implementation FAQs 5. Using polymorphic iterators in C++ Polymorphic: Allows you to change the aggregate class without changing the client code. Iterator object must be allocated dynamically by a factory method. Factory Method: Lets a class defer instantiation to subclasses. Difficult to implement in C++ for many reasons including: client is responsible for deleting the iterator (error-prone especially when there are multiple exit points); Can be remedied using the Proxy Pattern.

Implementation FAQs 6. Iterators may have privileged access. Iterator can be viewed as an extention of the aggregate that created it. Known as a friend of the aggregate. Such a relationship makes defining multiple traversals difficult because of the creation of multiple friends of the aggregate. Remedy: to allow the iterator access to protected operations for accessing important but publicly unavailable members of the aggregate.

Implementation FAQs 7. How are iterators used for composites? External iterators are difficult to implement over recursive aggregate structures like composites. The path to the current position must be stored. Internal Iterators can record the current position by calling itself recursively thereby storing the path implicitly in the call stack. If the nodes of a composite already have an interface for moving between siblings, parents, and children, a cursor based iterator would be better to just keep record of the current position.

Implementation FAQs 8. What about Null iterators? NullIterator : degenerate iterator that’s helpful for handling boundary conditions. Always done with traversal – IsDone operation always evaluates to true. Used to traverse tree-structured aggregates. At each position, the current element creates an iterator for its children. Aggregate elements return a concrete iterator as normal, but leaf elements return an instance of NullIterator making the implementation uniform.

Template class Iterator { Public: virtual void First() = 0; virtual void Next() = 0; virtual bool IsDone() const = 0; virtual Item CurrentItem() const = 0; Protected: Iterator(); } Sample Code Abstract Iterator Class Interface

Void PrintEmployees (Iterator & i) { for(i.First(); !i.IsDone(); i.Next()) { i.CurrentItem()->Print(); } Sample Code Using iterators to make printing easier Suppose you had a List of Employees with the employees class support the Print() operation. To print the list, you can define a PrintEmployees operation that takes an iterator as an argument: