© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Mid-Level Design Patterns: Iteration and Iterators.

Slides:



Advertisements
Similar presentations
Conquering Complex and Changing Systems Object-Oriented Software Engineering Chapter 12, Software Life Cycle.
Advertisements

General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Generator Design Patterns: The Factory Patterns.
Using Design Patterns in the Development of a Planner-Based Courseware System 作者: Khalid S. Al-Tahat, M.T. Sembok, and Sufian Bin Idris 出處: IEEE Region.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Chapter 3.2 C++, Java, and Scripting Languages. 2 C++ C used to be the most popular language for games Today, C++ is the language of choice for game development.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object, Object- Relational, and XML: Concepts, Models, Languages,
Chapter 1 Software Development. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 1-2 Chapter Objectives Discuss the goals of software development.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Interaction Design Process and Heuristics.
CASE Tools And Their Effect On Software Quality Peter Geddis – pxg07u.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Engineering Design Resolution & Design Principles.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
JAVA Introduction ● One of the main JAVA design goal is reducing complexity for programmer – Development time is half or less comparing to equivalent C++
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Detailed Design Overview and Mid-Level Class Modeling.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 A Discipline of Software Design.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Operation, Algorithm, and Data Structure Specification, and Design Finalization.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
Invitation to Computer Science, Java Version, Second Edition.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Reactor Design Patterns: Command and Observer.
SAMANVITHA RAMAYANAM 18 TH FEBRUARY 2010 CPE 691 LAYERED APPLICATION.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Patterns in Software Design.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Architectural Styles.
Object-Oriented Programming (OOP) CSC-2071 (3+1=4 Credits) Lecture No. 1 MBY.
Introduction and Features of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11.5 Lines and Curves in Space.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1 Functions.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Advanced UML Class Diagrams.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Design Reuse Earlier we have covered the re-usable Architectural Styles as design patterns for High-Level Design. At mid-level and low-level, design patterns.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Session 3 How to Approach the UML Written by Thomas A. Pender Published by Wiley Publishing, Inc. October 5, 2011 Presented by Kang-Pyo Lee.
CS 210 Iterator Pattern October 31 st, Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Prototyping.
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.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Product Design Alternative Generation, Evaluation, and Selection.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Mid-Level Design Patterns Categories.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Broker Design Patterns: Façade and Mediator.
Getting ready. Why C? Design Features – Efficiency (C programs tend to be compact and to run quickly.) – Portability (C programs written on one system.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Broker Design Patterns: Adapters and Proxy.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Functional Processing of Collections (Advanced) 6.0.
UML (Unified Modeling Language)
HIGH-LEVEL LANGUAGE PROGRAMMING PARADIGMS. Programming languages come in many forms or 'paradigms'. Each form of language offers advantages over other.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
High-level language programming paradigms. Programming languages come in many forms or 'paradigms'. Each form of language offers advantages over other.
Generator Design Patterns: Singleton and Prototype
Design Patterns: MORE Examples
MPCS – Advanced java Programming
Unified Modeling Language
Observer Design Pattern
JAVA Introduction ការណែនាំពី Java
Advanced Programming Behnam Hatami Fall 2017.
LESSON 01 Basebot Build. LESSON 01 Basebot Build.
SAMANVITHA RAMAYANAM 18TH FEBRUARY 2010 CPE 691
Presentation transcript:

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Mid-Level Design Patterns: Iteration and Iterators

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2 Objectives  To introduce the problem of collection iteration  To present a case study about how to design an iteration mechanism  To introduce the Iterator design pattern as an object-oriented realization of the best iteration mechanism design alternative  To present the structure, behavior, and uses of the Iterator pattern

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3 Topics  Collections and iteration  Iteration mechanisms  Iteration mechanism design alternatives  Selecting a design alternative  Iteration mechanism robustness  The Iterator pattern

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 Collections and Iteration A collection is an object that holds or contains other objects. Iteration over a collection or collection iteration is traversal and access of each element of a collection. A collection is an object that holds or contains other objects. Iteration over a collection or collection iteration is traversal and access of each element of a collection.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 Iteration Mechanisms  An iteration mechanism is a language feature or a set of operations that allow clients to access each element of a collection.  For example: Java and Visual Basic have for-loop constructs that support iteration over collections Several Java collections have size() and get() operations that allow element access in a standard for-loop

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6 Iteration Mechanism Operations  Initialize—Prepare the collection for traversal  Access current element—Provide client access to the current element  Advance current element—Move on to the next element in the collection  Completion test—Determine whether traversal is complete

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7 Other Iteration Mechanism Requirements  Information hiding—The internal structure of the collection must not be exposed.  Multiple simultaneous iteration—It must be possible to do more than one iteration at a time.  Collection interface simplicity—The collection interface must not be cluttered up with iteration controls.  Flexibility—Clients should have flexibility during collection traversal.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8 Iteration Mechanism Design Alternatives: Residence Iteration mechanism residence: Programming language—As in Java or Visual Basic  Depends on the language Collection—A built-in iteration mechanism resides in the collection Iterator—An external entity housing the iteration mechanism  An iterator is an entity that provides serial access to each elements of an associated collection.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9 Iteration Mechanism Design Alternatives: Control Iteration mechanism control: External iteration control—The iteration mechanism provides access to collection elements as directed by the client; the client calls the iteration control operations. Internal iteration control—The iteration mechanism accepts operations from clients that it applies to elements of the collection; the iteration mechanism calls the iteration control operations.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10 Iteration Mechanism Design Alternatives: Summary Residence CollectionIterator Control External Collection with built-in external control Iterator with external control Internal Collection with built-in internal control Iterator with internal control

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11 Built-In Internal Control: Implementation printObject( o : Object ) { print( o ) } … Collection c … c.apply( printObject )

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 Built-In Internal Control: Evaluation  Hides collection internals   Does not complicate the collection interface   Multiple simultaneous iteration is not easy   Client has little control over iteration—no flexibility 

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13 Built-In External Control: Implementation For each kind of iteration desired Add the iteration control operations (or their equivalents) to the collection Other operations may be needed to provide flexibility

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14 Built-In External Control: Evaluation  Hides collection internals   Greatly complicates the collection interface   Multiple simultaneous iteration is not easy   Client has control over iteration— adequate flexibility 

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15 Iterators with Internal Control  Hides collection internals   Does not unduly complicate the collection interface   Multiple simultaneous iteration is allowed   Client has little control over iteration—no flexibility 

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16 Iterators with External Control  Hides collection internals   Does not unduly complicate the collection interface   Multiple simultaneous iteration is allowed   Client has control over iteration—adequate flexibility   Iterators with external control is clearly the best design alternative—this is the basis for the Iterator design pattern.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17 Change During Iteration  What should happen when a collection is changed during iteration?  Requirements for a coherent iteration mechanism specification: Fault tolerance—The program should not crash. Iteration termination—Iteration should halt. Complete traversal—Elements always present should not be missed during traversal. Single access—No element should be accessed more than once.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18 Robust Iteration Mechanism  A robust iteration mechanism is one that conforms to some coherent specification of behavior when its associated collection changes during iteration.  Making an iteration mechanism robust may be difficult or expensive.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19 Iterator Pattern The Iterator pattern is an object- oriented design pattern for externally controlled iterators.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20 An Analogy Consider a warehouse full of items that a client must process one by one. Don’t allow clients into the warehouse (information hiding) Clerks are like iterators Clerks can fetch each item for clients (external control) Clerks can be instructed by the client and then process each element on their behalf (internal control)

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 21 Iterator Structure

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 22 Iterator Behavior

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 23 When to Use Iterators  Whenever traversing any collection Helps hide information Helps decouple code from particular collections (increases changeability) Simplifies collection interfaces Supports multiple concurrent iterations  Problems with iterators Robustness

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24 Summary 1  Collection iteration is an important task that can be realized by an iteration mechanism in many ways: Programming language feature Built-in to the collection with internal or external control Iterator with internal or external control  If an iteration mechanism is not part of the programming language, the best design alternative is an iterator with external control.

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25 Summary 2  The Iterator pattern is a model for an object-oriented implementation of an iterator with external control.  The Iterator pattern is well-known and widely implemented; it offers many advantages and should be the used whenever a collection is traversed.