the observer design pattern · the iterator design pattern •iterators are used to access the...

42
The Iterator Design Pattern Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation. An Iterator object encapsulates the internal structure of how the iteration occurs. An iterator will let you test if there are any more items (a hasNext method) and to get the next item (next method) Iterators can be applied to a variety of structures, such as a tree, linked list, hash table, and an array In the GoF classification this is a behavioral design pattern

Upload: others

Post on 15-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

The Iterator Design Pattern

• Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation.

• An Iterator object encapsulates the internal structure of how the iteration occurs.

• An iterator will let you test if there are any more items (a hasNext method) and to get the next item (next method)

• Iterators can be applied to a variety of structures, such as a tree, linked list, hash table, and an array

• In the GoF classification this is a behavioral design pattern

Page 2: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Acknowledgements

• Materials were borrowed from

– Design Patterns in Java by Steven Metsker and William Wake (textbook for course)

– Head First Design Patterns by Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra

– The main bullets on the previous slide came from the wikipedia article on the Iterator Pattern

Page 3: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

A Simple Example First

• Our first example comes from the Head First book; we need to iterate over menu items

Page 4: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Using Built-in Methods

• Using the get method associated with ArrayList

Page 5: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Now We Iterate over an Array

Page 6: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Let’s See How an Iterator Object Works

Page 7: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

It Works the Same Way for Arrays

Page 8: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

The Iterator Interface

• Let’s start developingsome code

Page 9: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

The Concrete Implementation

Page 10: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Modification to DinnerMenu Code

Page 11: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Changes to the Waitress

Page 12: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Let’s Test Our Code

Page 13: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
Page 14: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Expanding Our Iterator

Page 15: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
Page 16: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
Page 17: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
Page 18: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Java Iterators

• Iteration occurs frequently in Java

• As we have already seen, Iterators have methods hasNext(), next(), and remove()

• The program on the next page demonstrates this behavior

Page 19: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
Page 20: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Thread Safety

• This code displays a list of machines that are currently up

• This program uses sleep to simulate machines coming up while the program is displaying the list

Page 21: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

What Happens?• The run method modified the list while running in a

separate thread from the iterator

• Unfortunately the program crashes after printing a machine or two; a list that changes during iteration throws an exception

Page 22: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

A First Attempt at Fixing the Problem

• The previous exception will not be generated

• But we still need to examine if the program runs as intended

Page 23: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Let’s Examine the Output

• Here is the output

Page 24: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Solution 28.1

Page 25: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Synchronizing on the List

The use of synchronized

assures mutual exclusion

(mutex) on the list.

Page 26: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
Page 27: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
Page 28: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Solution 28.2

Page 29: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Iterating Over a Composite

Page 30: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

• CodeSnippets

Page 31: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

• More CodeSnippets

Page 32: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Manufacturing Processes

• Notice the use of a composite pattern

Page 33: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Some Code Snippets

Page 34: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Solution 28.3

Page 35: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Making Aerial Shells at Oozinoz

• Each leaf is an instance of ProcessStep

• The other nodes are instances of ProcessComposite

• Note the cyclic nature of the process

Page 36: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Using our Iterator

Page 37: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

• Addingindentingto output ofthe iterator

Page 38: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Our New Output

• This prinout is more readable

Page 39: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

• Returningonly leaves

Page 40: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

• More codesnippets

Page 41: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
Page 42: The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation

Solution 28.4