tddb84 design patterns lecture 04 more on iterators
play

TDDB84 Design Patterns Lecture 04 More on Iterators, Composite, - PowerPoint PPT Presentation

TDDB84 Design Patterns Lecture 04 More on Iterators, Composite, Abstract Factory Peter Bunus Department of Computer and Information Science Linkping University, Sweden peter.bunus@liu.se The Constitution of Software Architects


  1. TDDB84 Design Patterns Lecture 04 More on Iterators, Composite, Abstract Factory Peter Bunus Department of Computer and Information Science Linköping University, Sweden peter.bunus@liu.se

  2. The Constitution of Software Architects � Encapsulate what varies � Program through an interface not to an implementation � Favor Composition over Inheritance � Classes should be open for extension but closed for modification � Don ’ t call us, we ’ ll call you � ????????? � ????????? � ????????? � ????????? 2

  3. Iterating through menus printMenu() - prints every item on the menu printBreakfastMenu() - print just breakfast items printLunchMenu() - print just lunch items printVegetarianMenu() - print all vegetarian menu items isItemVegetarian() - given the name of an item, returns true is the item is vegetarian, otherwise returns false 3

  4. What did we do? We wanted to give to the ArrayList of Waiter an easy way to MenuItems iterate over more items 1 2 3 4 Our menu item had two different implementations and two different 1 interfaces for interacting ... and we didn’t want him 2 to know about how the menu items are implemented 3 An Array of 4 MenuItems 4

  5. We decoupled the Waiter So we gave the waiter an iterator for each group of objects he needed to iterate over... 1 2 3 4 Iterator next() 1 2 Iterator next() 3 Now he doesn’t have to worry about which 4 implementation we used; he always uses the same interface. He has been decoupled from the implementation 5 TDDB84 Design Patterns HT1 2009 LECTURE 04

  6. We Can Embrace Change HashTable Iterator Vector Java collections next() that already have an Iterator Iterator implemented LinkedList next() Iterator next() By giving him an Iterator we have decoupled him from the implementation of the menu items so we can easily add new menus if we want. 6 TDDB84 Design Patterns HT1 2009 LECTURE 04

  7. The Waitress Code 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); } private void printMenu(Iterator iterator) { while (iterator.hasNext()) { MenuItem menuItem = (MenuItem)iterator.next(); System.out.print(menuItem.getName() + ", "); System.out.print(menuItem.getPrice() + " -- "); 7 System.out.println(menuItem.getDescription()); TDDB84 Design Patterns HT1 2009 LECTURE 04 } }

  8. The Constitution of Software Architects � Encapsulate what varies � Program through an interface not to an implementation � Favor Composition over Inheritance � Classes should be open for extension but closed for modification � Don ’ t call us, we ’ ll call you � ????????? � ????????? � ????????? � ????????? 8

  9. The Waitress Code Revised 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(); public class MenuTestDrive { System.out.print(menuItem.getName() + ", "); public static void main(String args[]) { System.out.print(menuItem.getPrice() + " -- "); PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); System.out.println(menuItem.getDescription()); DinerMenu dinerMenu = new DinerMenu(); } Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu); } waitress.printMenu(); } waitress.printVegetarianMenu(); ... } ... } 9

  10. Design Principle Ahead Joe don ’ t do that. I feel that bad things will happen in the future if you do that Joe you could allow your aggregates to implement their internal collections and related operation AND the iteration methods. You could save some classes here 10

  11. Diner Menu Iterator public class DinerMenuIterator implements Iterator { MenuItem[] items; int position = 0; public DinerMenuIterator(MenuItem[] items) { this.items = items; } public Object next() { MenuItem menuItem = items[position]; position = position + 1; return menuItem; } public boolean hasNext() { if (position >= items.length || items[position] == null) { return false; } else { return true; } } } 11

  12. The Diner Menu Implementation public class DinerMenu { static final int MAX_ITEMS = 6; int numberOfItems = 0; MenuItem[] menuItems; int position=0; public DinerMenu() { menuItems = new MenuItem[MAX_ITEMS]; .... addItem("BLT","Bacon with lettuce & tomato on whole wheat", false, 2.99); ...... } public void addItem(String name, String description, boolean vegetarian, double price) { .... } } public Iterator createIterator() return new DinerMenuIterator(menuItems) public Object next() { MenuItem menuItem = menuItems[position]; position = position + 1; return menuItem; } public boolean hasNext() if (position >= menuItems.length || menuItems[position] == null) { return false; } else { 12 return true; TDDB84 Design Patterns HT1 2009 LECTURE 04 } } }

  13. The Star Trek Convention is in Town Joe there is a Stat Trek Convention in Town and the Klingonians would like to visit our restaurant. According to the Klingonian customs our waitress need to print them the menu backwards The Romulans are also coming in the evening. We need to give them some galactic snails, I sent you a new menu with galactic snail dishes. Print the menu as usual. 13 TDDB84 Design Patterns HT1 2009 LECTURE 04

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

  15. The Constitution of Software Architects � Encapsulate what varies � Program through an interface not to an implementation � Favor Composition over Inheritance � Classes should be open for extension but closed for modification Don ’ t call us, we ’ ll call you � A Class should have only one reason to change � � ????????? � ????????? � ????????? 15

  16. Violating the Single Responsability Principle maintainability reliability testability understandability High reusability Low 16

  17. Just when we thought it was safe... Joe, we need to insert a dessert menu into the Dinner menu. The kids will love that. Could you please fix that?

  18. The Desired Menu Structure All Menus ArrayList Coffee Menu 3 1 2 HashTable Dinner Menu Pancake Menu 1 2 1 2 3 4 3 ArrayList Array 4 1 Dessert Menu 2 3 4 18

  19. What we need All menus Coffee Menu Pancake House Menu Dinner Menu Dessert Menu MenuItem MenuItem MenuItem MenuItem MenuItem MenuItem MenuItem MenuItem MenuItem - A tree structure to accomodate menus - A way of traversing the tree - Traversing in a flexible manner (e.g. MenuItem MenuItem MenuItem MenuItem Traverse only the Diner’s dessert menu)

  20. Composite Peter Bunus Department of Computer and Information Science Linköping University, Sweden peter.bunus@liu.se

  21. The Composite Pattern The Composite Pattern allows us to build structures of objects in the � form of tree that contains both composition of objects and individual objects as nodes 21

  22. The Composite Explained The Client uses the Component interface to manipulate the objects in The Component defines an the commposition interface for all objects in the composition both the composite and the leaf node The Component may implement a default behavior for Add(), Remove(), GetChild() and its operations Note that Leaf will inherit Add(), Remove(), GetChild() which don’t make a lot of sense for a Leaf node A Leaf has no children The Composite’s role is to define behavior A Leaf defines the behavior for the of the components having children and to elements in the composition. It store child components. does this by implementing the the It also implements Leaf related operations. operations that the Composite Some of them might not make sense in supports Composite and exceptions might be 22 generated

  23. The Menu Composite The Waitress is going to MenuComponent use the MenuComponent represents the interface interface to access both for both MenuItem and Menus and MenuItems Menu. Methods for manipulating the components. The components are MenuItem and Menu MenuItem overrides the methods that make Menu also overrides the sense, an uses the methods that make default implementation in sense, like a way to add MenuComponent for and remove menu items. those that don’t make sense 23

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend