1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
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.
The Singleton Pattern II Recursive Linked Structures.
Java Review Interface, Casting, Generics, Iterator.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Behavioral Design Patterns May 5, 2015May 5, 2015May 5, 2015.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
Head-First Design Patterns. Similar to a ReminderEntry.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Fall 2007ACS Ron McFadyen1 Composite Pattern (see pages ) A composite is a group of objects in which some objects contain others; one object.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Winter 2015ACS Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent.
Tirgul OOP No.3 Iterators. What is an Iterator? An object that provides a way to access elements of an aggregate object sequentially, without exposing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
1 Topic 8 Iterators "First things first, but not necessarily in that order " -Dr. Who.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
CS 307 Fundamentals of Computer ScienceIterators 1 Topic 14 Iterators "First things first, but not necessarily in that order " -Dr. Who.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
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.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
LinkedList Many slides from Horstmann modified by Dr V.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
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.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Sit-In Lab 2 - OOP Restaurant.  Manage a restaurant and perform these types of queries: Assign a favorite table to a specific group Assign the lexicographically-smallest.
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.
Iteration Abstraction SWE Software Construction Fall 2009.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
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.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Iterators.
Template Method Pattern Iterator Pattern
Iterator Design Pattern
Iterator and Composite Design Patterns
"First things first, but not necessarily in that order " -Dr. Who
CSE 143 Lecture 27: Advanced List Implementation
Iterator.
Chapter 9 Behavioral Design Patterns
Behavioral Patterns Part-I introduction UNIT-VI
Menu item at a restaurant
Iteration Abstraction
Interator and Iterable
CIS 199 Final Review.
Software Design Lecture : 39.
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Presentation transcript:

1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar

2 Client Code Complex terrifying collection ?????

3 Client Code Complex terrifying collection Iterator hasnext() next() remove()

4 Intent Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation An aggregate object is an object that contains other objects for the purpose of grouping those objects as a unit. It is also called a container or a collection. Examples are a linked list. Also known as Cursor

5 Motivation An aggregate object such as a list should allow a way to traverse its elements without exposing its internal structure It should allow different traversal methods

6 Example public class MenuItem { String name; String description; boolean vegetarian; double price; public MenuItem(String name, String description boolean vegetarian, double price) { this.name=name; this.description=description; this.vegetarian=vegetarian; this.price=price; }

7 public String getName() { return name; } public String getDescription() { return description; } public double getPrice() { return price; } public boolean isVegetarian() { return vegetarian; }

8 Class LunchMenu: public class LunchMenu { ArrayList menuItems; public LunchMenu() { menuItems=new ArrayList(); // call addItem() method } public void addItem(String name, String description, boolean vegetarian, double price) { MenuItem menuItem=new MenuItem(name, description,vegetarian,price); menuItems.add(menuItem); }

9 public ArrayList getMenuItems() { return menuItems; } } Similarly there will be a class DinerMenu. The difference is that it is using an Array so in it we can control the maximum size of the menu.

10 To print all the items LunchMenu lunchMenu=new LunchMenu(); ArrayList lunchItems=lunchMenu.getMenuItems(); DinerMenu dinerMenu=new DinerMenu(); MenuItem[] dinerItems=dinerMenu.getMenuItems();

11 for(int i=0;i<lunchItems.size();i++) { MenuItem menuItem= (MenuItem)lunchItems.get(i); System.out.print(menuItem.getName()); System.out.println(menuItem.getPrice()); System.out.println(menuItem.getDescription()); } for(int i=0;i<dinerItems.length;i++) { MenuItem menuItem= dinerItems[i]; System.out.print(menuItem.getName()); System.out.println(menuItem.getPrice()); System.out.println(menuItem.getDescription()); }

12 import java.util.Iterator; public class DinerMenuIterator implements Iterator { MenuItem[] list; int position = 0; public DinerMenuIterator(MenuItems[] list) { this.list=list; } > Iterator hasNext() next() remove()

13 public Object next() { MenuItem menuItem=items[position]; position = position+1; return menuItem; } public boolean hasNext() { if(position >= items.length || items[position] == null) { return false; }

14 else { return true; } public void remove() { // to remove any item } Similarly we will have to create a LunchMenuIterator Class.

15 public interface Menu { public Iterator createIterator(); } Now we will implement this interface to both LunchMenu and DinerMenu class definition public class DinerMenu implements Menu { static final int MAX_ITEMS = 6; int numberOfItems = 0; MenuItem[] menuItems;

16 // addItem here public Iterator createIterator() { return new DinerMenuIterator(menuItems); } // other methods }

17 import java.util.Iterator; public class MenuPrint { Menu lunchMenu; Menu dinerMenu; public MenuPrint(Menu lunchMenu, Menu dinerMenu) { this.lunchMenu = lunchMenu; this.dinerMenu = dinerMenu; }

18 public void printMenu() { Iterator lunchIterator = lunchMenu.createIterator(); Iterator dinerIterator = dinerMenu.createIterator(); System.out.println(“Menu lunch”); printMenu(lunchIterator); System.out.println(“Diner”); printMenu(dinerIterator); }

19 private void printMenu(Iterator iterator) { while (iterator.hasNext()) { MenuItem menuItem = (MenuItem)iterator.next(); System.out.println(menuItem.getName()); System.out.println(menuItem.getPrice()); System.out.println(menuItem.getDescription()); }

20 Testing the code public class MenuTestDrive { public static void main(String args[]) { LunchMenu lunchMenu=new LunchMenu(); DinerMenu dinerMenu=new DinerMenu(); MenuPrint menuPrint=new MenuPrint (lunchMenu,dinerMenu); menuPrint.printMenu(); }

21 > Menu createIterator() > Iterator hasNext() next() remove() MenuPrint printMenu() LunchMenu menuItems createIterator() DinerMenuIterator hasNext() next() remove() DinerMenu menuItems createIterator() LunchMenuIterator hasNext() next() remove()

22 Structure > Aggregate createIterator() > Iterator hasNext() next() remove() Client ConcreteAggregate createIterator() ConcreteIterator hasNext() next() remove()

23 Applicability Use the Iterator pattern: To support traversals of aggregate objects without exposing their internal representation To provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration)

24 Participants Iterator - Defines an interface for accessing and traversing elements ConcreteIterator – - Implements the Iterator interface - Keeps track of the current position in the traversal Aggregate - Defines an interface for creating an Iterator object ConcreteAggregate - Implements the Iterator creation interface to return an instance of the proper ConcreteIterator

25 Observations Applications often "loop" over the same aggregate object in many classes and methods Changing from one to another is, as a result, very inconvenient The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure

26 Thanks……