Iterator Pattern




The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying
representation.


It also places the task of traversal on the iterator object, not on the aggregate, which simplifies the aggregate interface and implementation, and places the responsibility where it should be.

Example 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
public interface Menu {
 public Iterator createIterator();
}
==========
public class MenuItem {
 private String name;
 private String description;
 private boolean vegetarian;
 private double price;
 
 public MenuItem(String name, String desc, boolean vegetarian, double price){
  this.name = name;
  this.description = desc;
  this.vegetarian = vegetarian;
  this.price = price;
 }

  public String getName() {
  return name;
 }

  public String getDescription() {
  return description;
 }

  public boolean isVegetarian() {
  return vegetarian;
 }

  public double getPrice() {
  return price;
 }

  @Override
 public String toString() {
  return "MenuItem [name=" + name + ", description=" + description
    + ", vegetarian=" + vegetarian + ", price=" + price + "]";
 }
}
==========
public class BreakFastMenu implements Menu{
 private HashMap<String,MenuItem> breakFastMenuMap;
 BreakFastMenu(){
  breakFastMenuMap =  new HashMap<String,MenuItem>();
  addMenuItem("boiled_eggs","Boiled eggs and banana only",false,100);
  addMenuItem("juice","Mix fruit juice",true,40);
  addMenuItem("Salads and Curd","Salad and pure curd",true,70);
 }
 
 public void addMenuItem(String name, String description, boolean vegeratian, double price){
  MenuItem menuItem = new MenuItem(name, description, vegeratian, price);
  breakFastMenuMap.put(name, menuItem);
 }
 
 @Override
 public Iterator<MenuItem> createIterator() {
  return breakFastMenuMap.values().iterator();
 }
}
==========
import java.util.ArrayList;
import java.util.Iterator;
public class DinnerMenu implements Menu{
 private ArrayList<MenuItem> arrayList;
 DinnerMenu(){
  arrayList = new ArrayList<MenuItem>(10);
  addMenuItem("rotisabji", "Luch contains roti and subji and some curd", true, 40);
  addMenuItem("milk and roti", "Luch contains milk and roti only", true, 50);
  addMenuItem("chicken and rice", "Luch contains chicke and rince only", true, 50);
  addMenuItem("pau bhaji", "Luch pau bhaji and roasted papad", true, 60);
 }
 
 public void addMenuItem(String name, String description, boolean vegeratian, double price){
   MenuItem menuItem = new MenuItem(name, description, vegeratian, price);
   arrayList.add(menuItem);
 }
 
 public Iterator<MenuItem> createIterator(){
  return arrayList.iterator();
 }
}
==========
import java.util.Iterator;
public class LunchMenu implements Menu{
 private final int MAX_LUNCH_MENU_TEMS = 10;
 private MenuItem array[] ;
 private int availableTotalMenuItems = 0;
 
 LunchMenu(){
  array = new MenuItem[10];
  
  addMenuItem("rotisabji", "Luch contains roti and subji and some curd", true, 40);
  addMenuItem("milk and roti", "Luch contains milk and roti only", true, 50);
  addMenuItem("chicken and rice", "Luch contains chicke and rince only", true, 50);
  addMenuItem("pau bhaji", "Luch pau bhaji and roasted papad", true, 60);
 }
 
 public void addMenuItem(String name, String description, boolean vegeratian, double price){
  if(availableTotalMenuItems==MAX_LUNCH_MENU_TEMS){
   System.out.println("Menu is full. Maximul item in menu can be 10.");
  }else{
   MenuItem menuItem = new MenuItem(name, description, vegeratian, price);
   array[availableTotalMenuItems] = menuItem;
   availableTotalMenuItems++;
  }
 }

  public Iterator<MenuItem> createIterator(){
  LunchIterator iterator = new LunchIterator(array);
  return iterator;
 }
}
==========
import java.util.Iterator;
public class LunchIterator implements Iterator<MenuItem>{
 private MenuItem[] menuArray;
 private int index = 0;
 public LunchIterator(MenuItem[] arrayList){
  this.menuArray = arrayList;
 }
 
 @Override
 public boolean hasNext() {
  if(index>=menuArray.length || menuArray[index]==null){
   return false;
  }else{
   return true;
  }
 }

  @Override
 public MenuItem next() {
  MenuItem item = menuArray[index];
  index = index + 1;
  return item;
 }
}
==========
import java.util.Iterator;
public class Waitress {
 private Menu dinner;
 private Menu lunch;
 private Menu breakfast;
 
 public Waitress(Menu breakfast, Menu lunch,Menu dinner){
  this.breakfast =  breakfast;
  this.dinner = dinner;
  this.lunch = lunch;
 }
 
 public void printMenu(){
  System.out.println("MENU\n====");
  System.out.println("\nBREAKFAST\n========");
  Iterator breakfastIterator = breakfast.createIterator();
  printMenu(breakfastIterator);
  
  System.out.println("\nLUNCH\n====");
  Iterator lunchIterator = lunch.createIterator();
  printMenu(lunchIterator);
  
  System.out.println("\nDINNER\n=====");
  Iterator dinnerIterator = dinner.createIterator();
  printMenu(dinnerIterator);
 }
 
 private void printMenu(Iterator<MenuItem> iterator){
  while(iterator.hasNext()){
   MenuItem menuItem = (MenuItem)iterator.next();
   System.out.println(menuItem);
  }  
 }
}
==========
import java.util.Iterator;
public class LunchIterator implements Iterator<MenuItem>{
 private MenuItem[] menuArray;
 private int index = 0;
 public LunchIterator(MenuItem[] arrayList){
  this.menuArray = arrayList;
 }
 
 @Override
 public boolean hasNext() {
  if(index>=menuArray.length || menuArray[index]==null){
   return false;
  }else{
   return true;
  }
 }

  @Override
 public MenuItem next() {
  MenuItem item = menuArray[index];
  index = index + 1;
  return item;
 }
}
==========
public class IteratorFlight {
 public static void main(String[] universe){
  BreakFastMenu breakfast = new BreakFastMenu();
  LunchMenu lunch = new LunchMenu();
  DinnerMenu dinner = new DinnerMenu();
  Waitress waitress = new Waitress(breakfast, lunch, dinner);
  waitress.printMenu();
 }

}