Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar."— Presentation transcript:

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

2 2 Client Code Complex terrifying collection ?????

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

4 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 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 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 7 public String getName() { return name; } public String getDescription() { return description; } public double getPrice() { return price; } public boolean isVegetarian() { return vegetarian; }

8 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 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 10 To print all the items LunchMenu lunchMenu=new LunchMenu(); ArrayList lunchItems=lunchMenu.getMenuItems(); DinerMenu dinerMenu=new DinerMenu(); MenuItem[] dinerItems=dinerMenu.getMenuItems();

11 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 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 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 14 else { return true; } public void remove() { // to remove any item } Similarly we will have to create a LunchMenuIterator Class.

15 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 16 // addItem here public Iterator createIterator() { return new DinerMenuIterator(menuItems); } // other methods }

17 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 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 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 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 21 > Menu createIterator() > Iterator hasNext() next() remove() MenuPrint printMenu() LunchMenu menuItems createIterator() DinerMenuIterator hasNext() next() remove() DinerMenu menuItems createIterator() LunchMenuIterator hasNext() next() remove()

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

23 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 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 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 26 Thanks……


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

Similar presentations


Ads by Google