Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CS 210 Iterator Pattern October 31 st, 2006

2 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

3 Look at Eclipse code for Class MenuItem Class PancakeHouseMenu Class DinerMenu

4 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)

5 Iterating through breakfast items

6 Iterating through lunch items

7 Look at MenuTestDrive…

8 Encapsulating iteration Instead of using multiple ways to iterate through the lists, define ONE interface to iterate through the lists …

9 Using iterator to get breakfast items

10 Using iterator to get lunch items

11 Meet the iterator pattern…

12 DinerMenuIterator

13

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

15 Some improvements… Utilizing the Java provided implementation of iterator… Look at Eclipse…

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

17

18 Design Principle A class should have only one reason to change.

19 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.

20 Extending the Menu example with one more aggregate Cafemenu which uses Hashtable to implement the collection. Look at Eclipse.

21

22

23

24

25 Iterators and collections

26

27 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); }

28 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()); }


Download ppt "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."

Similar presentations


Ads by Google