Iterator - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Iterator

Description:

... must create either a new leaf node iterator or a new composite ... Leaf iterators do not have subiterators, so their depth is always 0. CompositeIterator ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 38
Provided by: mattg9
Category:
Tags: iterator | leafs | young

less

Transcript and Presenter's Notes

Title: Iterator


1
Iterator
  • Matt G. Ellis

2
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

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

4
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?

5
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?

6
Type Safe Collections
7
Type Safe Collections
8
Challenge 28.1
  • Fill in the missing code in ShowFireworkList to
    instantiate the iterator.

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

11
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)

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

13
Composites at Oozinoz
14
Composites at Oozinoz
15
Composites at Oozinoz
16
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.

17
Composite Iterator
18
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"

19
UML for CompositeIterator
20
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.

21
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?

22
Using the Iterator
23
Building the Iterator
24
LeafIterator
25
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.

26
CompositeIterator
27
CompositeIterator
  • Depth of a CompositeIterator is one plus the
    depth of its subiterator

28
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.

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

30
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?

31
Challenge 28.3
32
Challenge 28.3
33
Challenge 28.3
34
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

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

36
Thread Safety
  • What can we do to overcome this?
  • Metskers 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.

37
Questions?
Write a Comment
User Comments (0)
About PowerShow.com