Menu item at a restaurant

Slides:



Advertisements
Similar presentations
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Advertisements

Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Winter 2015ACS Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Go4 Visitor Pattern Presented By: Matt Wilson. Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct.
Chapter 9: The Iterator Pattern
程式語言結構 Final Project. Goal The student can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
LinkedList Many slides from Horstmann modified by Dr V.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
程式語言結構 Final Project 老師: Gwan-Hwan Hwang. Goal The students can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
CS 210 Iterator Pattern October 31 st, 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.
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Collections Dwight Deugo Nesa Matic
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Java Collections CHAPTER 3-February-2003
EKT472: Object Oriented Programming
Iterators.
Using the Java Collection Libraries COMP 103 # T2
Template Method Pattern Iterator Pattern
Sets Set is an unordered list of items
EECE 310: Software Engineering
MPCS – Advanced java Programming
Software Development Iterators
Observer Design Pattern
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Iterator Design Pattern
Iterator and Composite Design Patterns
Programming Design Patterns
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
ArrayLists.
ArraySet Methods and ArrayIterator
Iterator.
Lecture 26: Advanced List Implementation
Arrays versus ArrayList
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Behavioral Patterns Part-I introduction UNIT-VI
EE 422C Sets.
Lecture 2: Implementing ArrayIntList reading:
Chapter 8 Collection Types.
Object Oriented Programming in java
Iteration Abstraction
CS2110: Software Development Methods
slides created by Ethan Apter
Computer Science and Engineering
ArrayLists 22-Feb-19.
Interator and Iterable
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Software Design Lecture : 39.
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Java Collection interface add() clear() equals() hashCode() isEmpty() iterator() size() toArray() and a few more methods Programming Design Patterns 12/5/2018

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