Download - Unit 16 Iterator

Transcript
Page 1: Unit  16 Iterator

1

Unit 16Iterator

Summary prepared by Kirk Scott

Page 2: Unit  16 Iterator

2

Design Patterns in JavaChapter 28

Iterator

Summary prepared by Kirk Scott

Page 3: Unit  16 Iterator

3

The Introduction Before the Introduction

• This chapter is divided into 3 parts:• 1. Ordinary Iteration• 2. Thread-Safe Iteration• 3. Iterating over a Composite• I am going to dispense with each part in about

1 overhead

Page 4: Unit  16 Iterator

4

Ordinary Iteration

• This refers to the Iterator class with three methods: hasNext(), next(), and remove()

• The Iterable interface, which means a domain class has an iterator() method that returns an iterator on an instance of that class

• For each loops, which allow you to traverse the contents of a collection with an iterator

• This is all standard stuff, and it’s assumed that you are at least generally familiar with it

Page 5: Unit  16 Iterator

5

Thread-Safe Iteration

• This is beyond the scope of this course• No doubt it is a fascinating topic, full of tricks

and traps, but if you don’t know what threads are, it’s pointless to talk about iteration in that context

Page 6: Unit  16 Iterator

6

Iterating over a Composite

• This is the part of the chapter where the book develops some code, making the classes of the Composite design pattern implement the Iterable interface

• This means also developing a hierarchy of Iterator classes for components, leaves, and composites

• This is relatively complex

Page 7: Unit  16 Iterator

7

• As an introduction to iteration as a design pattern it would be better to implement iterability over a simpler programmer developed collection class

• For example, the general idea of a stack came up in the discussion of the Memento design pattern, so that data structure should be familiar

• Keep this in mind as an assignment or test question problem

Page 8: Unit  16 Iterator

8

The End

Page 9: Unit  16 Iterator

9

• All of the book diagrams for the chapter are tipped in on the following overheads for future reference

• They are not needed in this iteration of the overheads and you’re not responsible for what they contain

Page 10: Unit  16 Iterator

10

Page 11: Unit  16 Iterator

11

Page 12: Unit  16 Iterator

12

Page 13: Unit  16 Iterator

13


Top Related