130 likes | 225 Vues
Learn about ordinary iteration, thread-safe iteration, and iterating over a composite in Java. Understand Iterator class, Iterable interface, and developing classes for components, leaves, and composites. Dive into iteration as a design pattern with practical examples.
E N D
Unit 16Iterator Summary prepared by Kirk Scott
Design Patterns in JavaChapter 28Iterator Summary prepared by Kirk Scott
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
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
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
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
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
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