Iterator Design Pattern

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

ITEC200 Week04 Lists and the Collection Interface.
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.
Behavioral Design Patterns May 5, 2015May 5, 2015May 5, 2015.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
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.
Lexi case study (Part 2) Presentation by Matt Deckard.
程式語言結構 Final Project 老師: Gwan-Hwan Hwang. Goal The students can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
Chapter 7: The Adapter Pattern. Object Oriented Adapters Suppose that you have existing software. You have outsourced some of your work and there is a.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
CS 210 Iterator Pattern October 31 st, Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
CS Fall 2012, Lab 04 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /29/2016 Changes of Office Hours  Monday.
Collections Dwight Deugo Nesa Matic
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.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Advanced Programming 7/10/20161 Ananda Gunawardena.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Iterators.
Template Method Pattern Iterator Pattern
MPCS – Advanced java Programming
Chapter 19 Java Data Structures
Sequences and Iterators
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Observer Design Pattern
University of Central Florida COP 3330 Object Oriented Programming
Chapter 12: Data Structures
Iterator and Composite Design Patterns
Linked Lists.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Iterator.
Chapter 9 Behavioral Design Patterns
Object Oriented Design Patterns - Behavioral Patterns
Java Collections Framework
Menu item at a restaurant
And the list goes on and on and on….
Chapter 8 Collection Types.
Dynamic Data Structures and Generics
The iterator and memento patterns
Design Patterns Difficult to describe abstractly Elements:
Interator and Iterable
Dynamic Data Structures and Generics
Software Design Lecture : 39.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Introduction to Java Collection
Presentation transcript:

Iterator Design Pattern CECS277 Fall 2017 Mimi Opkins

Iterator Design Pattern This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation. Iterator pattern falls under behavioral pattern category.

Diner and Pancake House Merger Objectville diner and Objectville pancake house are merging into one entity. Thus, both menus need to merged. The problem is that the menu items have been stored in an ArrayList for the pancake house and an Array for the diner. Neither of the owners are willing to change their implementation.

Problems Suppose we are required to print every item on both menus. Two loops will be needed instead of one. If a third restaurant is included in the merger, three loops will be needed. Design principles that would be violated: Coding to implementation rather than interface The program implementing the joint print_menu() needs to know the internal structure of the collection of each set of menu items. Duplication of code

Solution Encapsulate what varies, i.e. encapsulate the iteration. An iterator is used for this purpose. The DinerMenu class and the PancakeMenu class need to implement a method called createIterator(). The Iterator is used to iterate through each collection without knowing its type (i.e. Array or ArrayList)

Original Iteration Getting the menu items: PancakeHouseMenu pancakeHouseMenu= new PancakeHouseMenu(); ArrayList breakfastItems = pancakeHouseMenu.getMenuItems(); DinerMenu dinerMenu = new DinerMenu(); MenuItems[] lunchItems = dinerMenu.getMenuItems(): Iterating through the breakfast items: for(int i=0; i < breakfastItems.size(); ++i) { MenuItem menuItem= (MenuItem) breakfastItems.get(i) } Iterating through the lunch items: for(int i=0; I < lunchItems.length; i++) MenuItem menuItem = lunchItems[i]

Using an Iterator Iterating through the breakfast items: Iterator iterator = breakfastMenu.createInstance(); while(iterator.hasNext()) { MenuItem menuItem = (MenuItem)iterator.next(); } Iterating through the lunch items: Iterator iterator = lunchMenu.createIterator();

Iterator Design Pattern The iterator pattern encapsulates iteration. The iterator pattern requires an interface called Iterator. The Iterator interface has two methods: hasNext() next() Iterators for different types of data structures are implemented from this interface.

Class Diagram for the Merged Diner

Using the Java Iterator Class Java has an Iterator class. The Iterator class has the following methods: hasNext() next() remove() If the remove() method should not be allowed for a particular data structure, a java.lang.UnsupportedOperationException should be thrown.

Improving the Diner Code Changing the code to use java.util.iterator: Delete the PancakeHouseIterator as the ArrayList class has a method to return a Java iterator. Change the DinerMenuIterator to implement the Java Iterator . Another problem - all menus should have the same interface. Include a Menu interface

Class Diagram Include Menu Adding the Menu interface

Iterator Pattern Definition Provides a way to access elements of a collection object sequentially without exposing its underlying representation. The iterator object takes the responsibility of traversing a collection away from collection object. This simplifies the collection interface and implementation. Allows the traversal of the elements of a collection without exposing the underlying implementation.

Iterator Pattern Class Diagram

Some Facts About the Iterator Pattern Earlier methods used by an iterator were first(), next(), isDone() and currentItem(). Two types of iterators: internal and external. An iterator can iterate forward and backwards. Ordering of elements is dictated by the underlying collection. Promotes the use of “polymorphic” iteration by writing methods that take Iterators as parameters. Enumeration is a predecessor of Iterator.

Design Principle If collections have to manage themselves as well as iteration of the collection this gives the class two responsibilities instead of one. Every responsibility if a potential area for change. More than one responsibility means more than one area of change. Thus, each class should be restricted to a single responsibility. Single responsibility: A class should have only one reason to change. High cohesion vs. low cohesion.

Iterators and Collections In Java the data structure classes form part of the Java collections framework. These include the ArrayList, Vector, LinkedList, Stack and PriorityQueue classes. Each of these classes implements the java.util.Collection interface which forces all subclasses to have an iterator() method. The Hashtable class contains keys and values which must iterated separately.

Credits http://titancs.ukzn.ac.za/Homepage.aspx