+ All Categories
Home > Documents > CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of...

CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of...

Date post: 06-Jan-2018
Category:
Upload: delphia-whitehead
View: 216 times
Download: 2 times
Share this document with a friend
Description:
Look at Eclipse code for Class MenuItem Class PancakeHouseMenu Class DinerMenu
28
CS 210 Iterator Pattern October 31 st , 2006
Transcript
Page 1: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

CS 210

Iterator PatternOctober 31st, 2006

Page 2: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Example to motivate discussion We have two lists (of menu items) one

implemented using ArrayList and another using Arrays.

How does one work with these two implementations of lists in a uniform way?

Example here uses restaurant items

Page 3: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Look at Eclipse code for Class MenuItem Class PancakeHouseMenu Class DinerMenu

Page 4: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Now if we want to… printMenu()

• Print every item on the menu printBreakfastMenu()

• Print just breakfast items printLunchMenu()

• Print just lunch items printVegetarianMenu() isItemVegetarian(name)

Page 5: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Iterating through breakfast items

Page 6: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Iterating through lunch items

Page 7: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Look at MenuTestDrive…

Page 8: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Encapsulating iteration Instead of using multiple ways to iterate

through the lists, define ONE interface to iterate through the lists …

Page 9: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Using iterator to get breakfast items

Page 10: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Using iterator to get lunch items

Page 11: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Meet the iterator pattern…

Page 12: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

DinerMenuIterator

Page 13: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Page 14: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Look at… Code for DinerMenuIterator Code for PancakeHouseMenuIterator Code for Waitress

Page 15: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Some improvements… Utilizing the Java provided

implementation of iterator… Look at Eclipse…

Page 16: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Iterator Pattern defined

The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Page 17: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Page 18: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Design Principle

A class should have only one reason to change.

Page 19: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Design principle applied to Iterator pattern Iterator patter places the task of traversal

on the iterator object, not on the aggregate, which simplifies the aggregate interface and implementation, and places the responsibility where it should be.

Page 20: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Extending the Menu example with one more aggregate Cafemenu which uses Hashtable to

implement the collection. Look at Eclipse.

Page 21: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Page 22: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Page 23: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Page 24: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Page 25: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Iterators and collections

Page 26: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Page 27: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

A look at the waitress class public class Waitress {

Menu pancakeHouseMenu;Menu dinerMenu;Menu cafeMenu;

public Waitress(Menu pancakeHouseMenu, Menu dinerMenu, Menu cafeMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;this.cafeMenu = cafeMenu;}

public void printMenu() {Iterator pancakeIterator = pancakeHouseMenu.createIterator();Iterator dinerIterator = dinerMenu.createIterator();Iterator cafeIterator = cafeMenu.createIterator();

System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);System.out.println("\nDINNER");printMenu(cafeIterator);}

Page 28: CS 210 Iterator Pattern October 31 st, 2006. Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.

Removing dependence on specific menu items… public class Waitress {

ArrayList menus;

public Waitress(ArrayList menus) {this.menus = menus;}

public void printMenu() {Iterator menuIterator = menus.iterator();while(menuIterator.hasNext()) {Menu menu = (Menu)menuIterator.next();printMenu(menu.createIterator());}}

void printMenu(Iterator iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();System.out.print(menuItem.getName() + ", ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}}

}


Recommended