Download presentation
Presentation is loading. Please wait.
1
Menu item at a restaurant
public class MenuItem { String name, description; double price; boolean vegetarian; public MenuItem(String name, …) { …} public String getName() { …} public String getDescription() {…} public double getPrice() { …} public boolean isVegetarian() { …} } Programming Design Patterns 12/5/2018
2
Programming Design Patterns
PancakeHouse public class PancakeHouseMenu { ArrayList menuItems; public PanckageHouseMenu() { // initialize menuItems with various items } public void addItem(…) { …} public ArrayList getMenuItems() {…} // other methods Programming Design Patterns 12/5/2018
3
Programming Design Patterns
DinerMenu public class DinerMenu { MenuItems[] menuItems; public DinerMenu() { // initialize menuItems with various items } public void addItem(…) { …} public MenuItem[] getMenuItems() {…} // other methods Programming Design Patterns 12/5/2018
4
Combining the two menus
Write code to print the menu items of both the PancakeHouse and Diner CLASSWORK: printCombinedMenu(…) assume references pankItems and dinerItems size of an arrayList: pankItems.size() accessing element in an ArrayList: pankItems.get(i); size of an array: dinerItems.length accessing element in an array: dinerItems[i]; Programming Design Patterns 12/5/2018
5
Programming Design Patterns
printCombinedMenu() ArrayList pankItems = pancakeHouseMenu.getMenuItems(); MenuItem[] dinerItems = dinerMenu.getMenuItems(); for (int i=0; i<pankItems.size(); i++) { MenuItem menuItem = (MenuItem)pankItems.get(i); // print details } for (int i=0; i<dinerItems.length; i++) { MenuItem menuItem = (MenuItem)dinerItems[i]; Combine the two for loops into one Programming Design Patterns 12/5/2018
6
Combining the two menus
Why can’t the two for loops be combined into one? What code will change if another menu list is added to this combined menu restaurant? What design principles are violated in the printCombinedMenu() method? The size of the two menus is different. The way to step through an array and arrayList is different? Need to add for loops for each new menu Programming Design Patterns 12/5/2018
7
Combining the two menus
Why can’t the two for loops be combined into one? The size of the two menus is different. The way to step through an array and arrayList is different What code will change if another menu is added to this combined menu restaurant? a new for loop will have to be added Design Principles? Programming Design Patterns 12/5/2018
8
Combined Menu: Design principles
PancakeHouseMenu and DinerMenu are concrete implementations should program to an interface If the underlying storage of DinerMenu or PancakeHouseMenu changes, to a Hashtable for example, the combined menu code will also have to change Bad design. Exposing the underlying implementation. Duplicate code in the for loops. encapsulate what varies in the for loop Programming Design Patterns 12/5/2018
9
Program to an interface
How can the two for loops be combined into one? What code will change if another menu is added to this combined menu restaurant? Programming Design Patterns 12/5/2018
10
Program to an interface
Why can’t the two for loops be combined into one? The size of the two menus is different. The collections should implement an interface with a method hasNext() The way to step through an array and arrayList is different? Let another object be responsible for stepping through What code will change if another menu is added to this combined menu restaurant? a new for loop will have to be added Use polymorphism (method overloading wherein the parameter is an interface) so that the same for loop can be used Programming Design Patterns 12/5/2018
11
Programming Design Patterns
Iterator Interface public interface Iterator { boolean hasNext(); Object next(); } Note Collections: a group of objects stored in data structures such as lists, arrays, hashtables, etc. Also called as Aggregates Programming Design Patterns 12/5/2018
12
Programming Design Patterns
Idea of Iterators Iterator iterator = pancakeHouseMenu.createIterator(); while(iterator.hasNext()) { MenuItem menuItem = (MenuItem)iterator.next(); } Iterator iterator = dinerMenu.createIterator(); Programming Design Patterns 12/5/2018
13
Programming Design Patterns
PancakeHouse public class PancakeHouseMenu { ArrayList menuItems; public PanckageHouseMenu() { // initialize menuItems with various items } public void addItem(…) { …} public ArrayList getMenuItems() {…} // other methods CLASSWORK: Modify code so that it returns an Iterator Programming Design Patterns 12/5/2018
14
Programming Design Patterns
PancakeHouse public class PancakeHouseMenu { ArrayList menuItems; public PanckageHouseMenu() { // initialize menuItems with various items } public void addItem(…) { …} public ArrayList getMenuItems() {…} public Iterator createIterator() { return new PancakeHouseIterator(menuItems); // other methods CLASSWORK: Need to implement PancakeHouseIterator Programming Design Patterns 12/5/2018
15
Programming Design Patterns
DinerMenu public class DinerMenu { MenuItems[] menuItems; public DinerMenu() { // initialize menuItems with various items } public void addItem(…) { …} public MenuItem[] getMenuItems() {…} // other methods CLASSWORK: Modify code so that it returns an Iterator Programming Design Patterns 12/5/2018
16
Programming Design Patterns
DinerMenu public class DinerMenu { MenuItems[] menuItems; public DinerMenu() { // initialize menuItems with various items } public void addItem(…) { …} public MenuItem[] getMenuItems() {…} public Iterator createIterator() { return new DinerMenuIterator(menuItems); // other methods Programming Design Patterns 12/5/2018
17
Programming Design Patterns
Combined Menu Class Write a combinedMenu class In the constructor pass PancakeHouseMenu and DinerMenu Write code for the printMenu(…) method Page 328 in the book Programming Design Patterns 12/5/2018
18
Programming Design Patterns
Combined Menu Class public class CombinedMenus { PancakeHouseMenu pMenu; DinerMenu dMenu; public CombinedMenus(PancakeHouseMenu pMenuIn, DinerMenu dMenuIn) { …// } public void printMenu() { // get iterator from each of the data members // call another method that just takes an Iterator Page 328 in the book Programming Design Patterns 12/5/2018
19
Programming Design Patterns
printMenu public void printMenu() { Iterator pIterator = pMenu.getIterator(); Iterator dIterator = dMenu.getIterator(); printMenu(pIterator); printMenu(dIterator) } public void printMenu(Iterator iterator) { // CLASSWORK: write code for this method Page 328 in the book Programming Design Patterns 12/5/2018
20
Programming Design Patterns
printMenu public void printMenu(Iterator iterator) { while (iterator.hasNext()) { MenuItem mItem = (MenuItem)iterator.next(); // print name, description, … from mItem } Page 328 in the book Programming Design Patterns 12/5/2018
21
How are these questions answered by Iterators?
Why can’t the two for loops be combined into one? What code will change if another menu is added to this combined menu restaurant? What design principles are violated followed in the printCombinedMenu() method? Programming Design Patterns 12/5/2018
22
Dependence on Concrete Implementations
The code (slide 19) still depends on concrete implementations where? Note the use of polymorphism/method-overloading for printMenu(…) Is the waitres code decoupled from DinerMenu and PancakeHouseMenu classes? page 328 from the book The data members should be Menu pancakeHouseMenu; The constructor should take two parameters of interface Menu Yes, it is decoupled if the code is written to the interface Menu, not concrete implementations. Programming Design Patterns 12/5/2018
23
CombinedMenus code: common interface
public class CombinedMenus { PancakeHouseMenu pMenu; DinerMenu dMenu; // … } Use a Menu interface to remove dependency on concrete implementations Menu should have the createIterator() method Programming Design Patterns 12/5/2018
24
Programming Design Patterns
Improved code Homework Study the code in page 344 of the book Change data member to be an ArrayList of menus Change printMenu() code accordingly it should retrieve an iterator for the arrayList for each element obtained from the iterator retrieve another iterator pass it to the printMenu() code Programming Design Patterns 12/5/2018
25
Programming Design Patterns
Iterator Provides a way to step through the elements of an aggregate without forcing the aggregate to clutter its own interface with a bunch of methods for traversal Implementation of the Iterator lives outside of the aggregate object iterator encapsulates the iteration In our examples, the aggregates are responsible for creating their own iterators Programming Design Patterns 12/5/2018
26
Java’s Iterator Interface
Has 3 methods hasNext() next() remove() Multithreading: read the documentation on behavior when multiple threads are accessing the collection some are internally synchronized Programming Design Patterns 12/5/2018
27
Programming Design Patterns
Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation Uniform way of accessing aggregate objects Can write polymorphic code that works with any aggregate Responsibility of iteration is not with the aggregate object, but with the iterator Design principle? Next slide … A class should have only one reason to change Programming Design Patterns 12/5/2018
28
Programming Design Patterns
Design Principle A class should have only one reason to change aggregate should just manage its data not be concerned about providing functionality to traverse Cohesion high cohesion: class is designed around a set of related functions low cohesion: class is designed around a set of unrelated functions a class that returns current temperature and stock quote Programming Design Patterns 12/5/2018
29
Java Collection interface
add() clear() equals() hashCode() isEmpty() iterator() size() toArray() and a few more methods Programming Design Patterns 12/5/2018
30
Programming Design Patterns
Java 5 Don’t have to directly use an Iterator Has a new for/in construct for (Object obj: collection) { } for (MenuItem item: items) { String description = item.getDescription(); System.out.println(“Item is: “ + description); Programming Design Patterns 12/5/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.