Mid level design patterns iteration and iterators
Download
1 / 25

Mid-Level Design Patterns: Iteration and Iterators - PowerPoint PPT Presentation


  • 154 Views
  • Updated On :

Mid-Level Design Patterns: Iteration and Iterators. Objectives. To introduce the problem of collection iteration To present a case study about how to design an iteration mechanism

Related searches for Mid-Level Design Patterns: Iteration and Iterators

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Mid-Level Design Patterns: Iteration and Iterators' - johnna


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Objectives
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


Topics
Topics

  • Collections and iteration

  • Iteration mechanisms

  • Iteration mechanism design alternatives

  • Selecting a design alternative

  • Iteration mechanism robustness

  • The Iterator pattern


Collections and iteration
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.


Iteration mechanisms
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


Iteration mechanism operations
Iteration Mechanism Operations

  • Initialize—Prepare the collection for traversal

  • Access currentelement—Provide client access to the current element

  • Advance currentelement—Move on to the next element in the collection

  • Completiontest—Determine whether traversal is complete


Other iteration mechanism requirements
Other Iteration Mechanism Requirements

  • Informationhiding—The internal structure of the collection must not be exposed.

  • Multiple simultaneousiteration—It must be possible to do more than one iteration at a time.

  • Collection interfacesimplicity—The collection interface must not be cluttered up with iteration controls.

  • Flexibility—Clients should have flexibility during collection traversal.


Iteration mechanism design alternatives residence
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.


Iteration mechanism design alternatives control
Iteration Mechanism Design Alternatives: Control

Iteration mechanism control:

  • External iterationcontrol—The iteration mechanism provides access to collection elements as directed by the client; the client calls the iteration control operations.

  • Internal iterationcontrol—The iteration mechanism accepts operations from clients that it applies to elements of the collection; the iteration mechanism calls the iteration control operations.



Built in internal control implementation
Built-In Internal Control: Implementation

printObject( o : Object ) {

print( o )

}

Collection c

c.apply( printObject )


Built in internal control evaluation
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 


Built in external control implementation
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


Built in external control evaluation
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 


Iterators with internal control
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 


Iterators with external control
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.


Change during iteration
Change During Iteration

  • What should happen when a collection is changed during iteration?

  • Requirements for a coherent iteration mechanism specification:

    • Faulttolerance—The program should not crash.

    • Iterationtermination—Iteration should halt.

    • Completetraversal—Elements always present should not be missed during traversal.

    • Singleaccess—No element should be accessed more than once.


Robust iteration mechanism
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.


Iterator pattern
Iterator Pattern

The Iterator pattern is an object-oriented design pattern for externally controlled iterators.


An analogy
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)




When to use iterators
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


Summary 1
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.


Summary 2
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.


ad