From the course: Programming Foundations: Design Patterns
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Using the Iterator pattern
From the course: Programming Foundations: Design Patterns
Using the Iterator pattern
- [Instructor] Let's take a look at the class diagram for the iterator pattern and then we'll see how to implement it for the menus. In our example, the aggregate object is a menu which uses an array list or an array to store the menu items. We have an aggregate interface and a concrete aggregate class that implements that interface, providing a method, create iterator, that creates and returns an iterator object. The client stores that iterator and then, when it needs to iterate over the items in the aggregate object, it can use the methods defined by the iterator interface to do so. The hasNext method figures out if there are more items to iterate over and the next method just returns the next item. Here's how we'll design our menus example to use the iterator pattern. Our aggregate object will be the menu. We'll add a menu interface that defines one method, create iterator, and both the pancake house menu and the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Encapsulating iteration1m 44s
-
(Locked)
Understanding the Iterator pattern2m 28s
-
(Locked)
Using the Iterator pattern3m 36s
-
(Locked)
Using built-in iterators1m 19s
-
(Locked)
The single responsibility principle1m 20s
-
(Locked)
The iterator pattern as language feature1m 30s
-
(Locked)
Challenge: The Iterator pattern57s
-
(Locked)
Solution: The Iterator pattern36s
-
(Locked)
-
-
-