Iterator mode: Provides a way to sequentially access individual elements in an aggregated object without exposing their internal representations.
The Pancake House and the restaurant are merged! But there was a small problem, although both agreed to implement the same menu item MenuItem, but the Pancake House wanted to use ArrayList to store the menu items, while the restaurant used an array, for the waitress to have access to the two menu at the same time, we needed to provide a unified access interface for the menu.
First look at the menu item MenuItem, the two stores are implemented the same
classMenuItem {stringName//name stringDescription//Description BOOLVegetarian;//whether it is vegetarian DoublePrice//Price PublicMenuItem (stringNamestringDescriptionBOOLVegetarian,DoublePrice ) { This. Name =name; This. Description =description; This. Vegetarian =Vegetarian; This. Price =Price ; } Public stringGetName () {returnname; } Public stringgetdescription () {returndescription; } Public DoubleGetPrice () {returnPrice ; } Public BOOLIsvegetarian () {returnVegetarian; } }
Interface menu, which defines only a method for creating iterators, the classes that implement this interface will provide their own different iterators
Interface Menu { Iterator createiterator (); }
Pancake House menu, implements the menus interface
classPancakehousemenu:menu {ArrayList MenuItems; PublicPancakehousemenu () {MenuItems=NewArrayList (); AddItem ("k&b ' s pancake breakfast","pancakes with scrambled Eggs,and toast",true,2.99); AddItem ("Regular Pancake Breakfast","pancakes with fired Eggs,and sausage",false,2.99); AddItem ("Blueberry Pancake","pancakes with fresh blueberries",true,3.49); AddItem ("Waffles","waffles,with your choice of blueberries or strawberries",true,3.59); } Public voidAddItem (stringNamestringDescriptionBOOLVegetarian,DoublePrice ) {MenuItem MenuItem=NewMenuItem (name, description, vegetarian, price); Menuitems.add (MenuItem); } PublicIterator Createiterator () {return NewPancakehouseiterator (MenuItems); } //Other ways to menu}
The same is true for restaurant menus
classDinermenu:menu {Static intMax_items =6; intNumberofitems =0; Menuitem[] MenuItems; PublicDinermenu () {MenuItems=NewMenuitem[max_items]; AddItem ("Vegetarian BLT","(Fakin ') Bacon with lettuce & tomato on whole wheat",true,2.99); AddItem ("BLT","Bacon with lettuce & tomato on whole wheat",false,2.99); AddItem ("Soup of the day","Soup of the day,whit a side of potato salad",false,3.29); AddItem ("Hot Dog","A hot Dog, with saurkraut, relish, onions, topped with cheese",false,3.05); } Public voidAddItem (stringNamestringDescriptionBOOLVegetarian,DoublePrice ) {MenuItem MenuItem=NewMenuItem (name, description, vegetarian, price); if(Numberofitems >=Max_items) {Console.WriteLine ("Sorry,menu is full! Can ' t Add item to menu!"); } Else{Menuitems[numberofitems]=MenuItem; Numberofitems++; } } Publicmenuitem[] Getmenuitems () {returnMenuItems; } PublicIterator Createiterator () {return NewDinermenuiterator (MenuItems); } //Other ways to menu}
Next look at the iterator, the iterator has an interface that defines some operations
Interface Iterator { bool hasnext (); Object next (); // more methods, such as Remove... }
Different menus have different iterators, this is the Pancake house
classPancakehouseiterator:iterator {ArrayList items; intPosition =0; PublicPancakehouseiterator (ArrayList items) { This. Items =items; } Public ObjectNext () {MenuItem MenuItem=(MenuItem) items[position]; Position++; returnMenuItem; } Public BOOLHasnext () {if(position>=items. Count) {return false; } Else { return true; } } }
It's a restaurant.
classdinermenuiterator:iterator {menuitem[] items; intPosition =0; Publicdinermenuiterator (menuitem[] items) { This. Items =items; } Public ObjectNext () {MenuItem MenuItem=Items[position]; Position++; returnMenuItem; } Public BOOLHasnext () {if(Position>=items. length| | items[position]==NULL) { return false; } Else { return true; } } }
Choosing a waitress is a convenient way to operate two menus in a unified manner.
classWaitress {Menu pancakehousemenu; Menu Dinermenu; PublicWaitress (menu pancake, menu diner) { This. Pancakehousemenu =Pancake; This. Dinermenu =Diner; } Public voidPrintmenu () {Iterator pancakeiterator=Pancakehousemenu.createiterator (); Iterator Dineriterator=Dinermenu.createiterator (); Console.WriteLine ("menu\n----\nbreakfat"); Printmenu (Pancakeiterator); Console.WriteLine ("\nlunch"); Printmenu (Dineriterator); } Private voidPrintmenu (Iterator Iterator) { while(Iterator.hasnext ()) {MenuItem MenuItem=(MenuItem) iterator.next (); Console.Write (Menuitem.getname ()+","); Console.WriteLine (Menuitem.getprice ()+"-- "); Console.WriteLine (" "+menuitem.getdescription ()); } } }
The benefit of iterators is that if a new menu is added in the future, and it uses a different storage method (for example, Hashtable), it only needs to implement the iterator interface without modifying the way the waitress operates.
"Head First design mode" C # implementation (iv)--iterator mode