The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the...

Post on 15-Jul-2020

3 views 0 download

Transcript of The Observer Design Pattern · The Iterator Design Pattern •Iterators are used to access the...

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

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

A Simple Example First

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

Using Built-in Methods

• Using the get method associated with ArrayList

Now We Iterate over an Array

Let’s See How an Iterator Object Works

It Works the Same Way for Arrays

The Iterator Interface

• Let’s start developingsome code

The Concrete Implementation

Modification to DinnerMenu Code

Changes to the Waitress

Let’s Test Our Code

Expanding Our Iterator

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

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

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

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

Let’s Examine the Output

• Here is the output

Solution 28.1

Synchronizing on the List

The use of synchronized

assures mutual exclusion

(mutex) on the list.

Solution 28.2

Iterating Over a Composite

• CodeSnippets

• More CodeSnippets

Manufacturing Processes

• Notice the use of a composite pattern

Some Code Snippets

Solution 28.3

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

Using our Iterator

• Addingindentingto output ofthe iterator

Our New Output

• This prinout is more readable

• Returningonly leaves

• More codesnippets

Solution 28.4