The body of the
A, definitions,
The iterator pattern provides a way to sequentially access elements in an aggregate object without exposing their internal representation.
Key points:
- The iterator pattern places the responsibility for moving between elements on the iterator, not the aggregate object. This simplifies the interface and implementation of aggregation and puts responsibilities in their place.
Two, implementation steps
Create an iterator interface
Public Boolean hasNext(); public Boolean hasNext(); public Boolean hasNext(); Public Object next(); }Copy the code
2. Create concrete iterators and implement the iterator interface
Concrete iterators are responsible for iterating through elements and managing where they are currently iterated.
ConcreteIterator implements Iterator public Class ConcreteIterator implements Iterator {/** * public String[] items; /** * int position = 0; public ConcreteIterator(String[] items) { this.items = items; } @Override public boolean hasNext() { if (position < items.length) { return true; } return false; } @Override public Object next() { if (! this.hasNext()) { return null; } String item = items[position]; position = position + 1; return item; }}Copy the code
Create an aggregate interface and define methods to return iterators
/** * public interface Aggregate {/** * public createIterator(); }Copy the code
4. Create concrete aggregates and implement methods that return iterators
Concrete aggregates hold sets in them. A collection here refers to a group of objects that can be stored as a list, array, hash table, etc.
/** * ConcreteAggregate implements Aggregate {/** * public String[] Aggregate = new String[] {"item1", "item2", "item3"}; @Override public Iterator createIterator() { return new ConcreteIterator(items); }}Copy the code
5. Use iterators for traversal
Public class Test {public static void main(String[] args) {// Aggregate Aggregate = new ConcreteAggregate(); Iterator Iterator = aggregate. CreateIterator (); While (iterator.hasnext ()) {system.out.println (iterator.next()); }}}Copy the code
Three, for example
1, the background
Object Village Restaurant and Object Village Pancake House have merged and now we can enjoy pancake House’s delicious pancake breakfast and delicious restaurant lunch in the same place.
Let’s say you’re hired by a new company they’ve assembled to create a Java version of a waitress. This Java version of the waitress specification: the ability to print customized menus in response to customer needs without asking the chef.
Now, there’s a slight snub: The pancake House menu uses an ArrayList to record menu items, while the restaurant uses an array to record menu items. Both are reluctant to change their implementations because so much code depends on them.
The good news is that both pancake house and restaurant have agreed to implement a single MenuItem, MenuItem.
2, implementation,
Pass the traversal of the collection to the iterator, so you don’t care whether the menu uses an ArrayList or an array to record menu items.
(1) Create menu items
/** * MenuItem */ public class MenuItem {String name; double price; public MenuItem(String name, double price) { this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; }}Copy the code
(2) Create iterator interface
Public Boolean hasNext(); public Boolean hasNext(); public Boolean hasNext(); Public Object next(); }Copy the code
(3) Create a concrete menu iterator
PancakeHouseMenuIterator */ public class PancakeHouseMenuIterator implements Iterator {/** * list of menu items */ public ArrayList<MenuItem> menuItems; int position = 0; public PancakeHouseMenuIterator(ArrayList<MenuItem> items) { this.menuItems = items; } @Override public boolean hasNext() { if (position < menuItems.size()) { return true; } return false; } @Override public Object next() { if (! this.hasNext()) { return null; } MenuItem item = menuItems.get(position); position = position + 1; return item; }}Copy the code
/** * implements Iterator {/** * implements Iterator public MenuItem[] menuItems; int position = 0; public DinerMenuIterator(MenuItem[] items) { this.menuItems = items; } @Override public boolean hasNext() { if (position < menuItems.length) { return true; } return false; } @Override public Object next() { if (! this.hasNext()) { return null; } MenuItem item = menuItems[position]; position = position + 1; return item; }}Copy the code
(4) Create menu interface
Public interface Menu {/** * createIterator */ public createIterator(); }Copy the code
(5) Create a specific menu
*/ public class PancakeHouseMenu implements Menu {public ArrayList<MenuItem> menuItems; public PancakeHouseMenu() { menuItems = new ArrayList<MenuItem>(); MenuItems. Add (new MenuItem("Regular Pancake Breakfast", 2.99)); MenuItems. Add (new MenuItem (" Blueberry Pancakes ", 3.49)); MenuItems. Add (new MenuItem (3.59) "Waffles,"); } @Override public Iterator createIterator() { return new PancakeHouseMenuIterator(menuItems); }}Copy the code
/** * public implements Menu {public menuItems [] menuItems; public DinerMenu() { menuItems = new MenuItem[3]; MenuItems [0] = new MenuItem("BLT", 2.99); MenuItems [1] = new MenuItem("Soup of the day", 3.29); MenuItems [2] = New MenuItem("Hotdog", 3.05); } @Override public Iterator createIterator() { return new DinerMenuIterator(menuItems); }}Copy the code
(6) Create hostesses
/** * public class Waitress {Menu pancakeHouseMenu; Menu dinerMenu; public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) { this.pancakeHouseMenu = pancakeHouseMenu; this.dinerMenu = dinerMenu; } public void printMenu () {/ / access to menu Iterator Iterator pancakeIterator = pancakeHouseMenu. CreateIterator (); Iterator dinerIterator = dinerMenu.createIterator(); / / use iterators to print MENU System. Out. The println (" -- MENU - \ n "); System.out.println("BREAKFASE:"); printMenu(pancakeIterator); System.out.println("\nLUNCH:"); printMenu(dinerIterator); } private void printMenu(Iterator iterator) { while (iterator.hasNext()) { MenuItem menuItem = (MenuItem)iterator.next(); System.out.println(menuItem.getName() + ", " + menuItem.getPrice()); }}}Copy the code
(7) Use hostesses to print menus
Public class Test {public static void main(String[] args) {Menu pancakeHouseMenu = new pancakeHouseMenu (); Menu dinerMenu = new DinerMenu(); // Waitress Waitress = new Waitress(pancakeHouseMenu, dinerMenu); // printMenu waitress. PrintMenu (); }}Copy the code