Design Pattern-composite pattern iterator (iterator)

Source: Internet
Author: User
Detailed description of composite pattern iterator


Address: http://blog.csdn.net/caroline_wendy


Reference composite pattern: http://blog.csdn.net/caroline_wendy/article/details/36895627


Add the iterator function in composite pattern to traverse the items of each composite.


Specific Method:

1. Abstract component class (Abstract component) method for creating an iterator.

/*** @ Time July 4, 2014 */package composite; import Java. util. iterator;/*** @ author C. l. wang **/public abstract class menucomponent {public void add (menucomponent) {Throw new unsupportedoperationexception (); // if not provided, you cannot call} public void remove (menucomponent) {Throw new unsupportedoperationexception ();} public menucomponent getchild (int I) {Throw new unsupportedoperationexception ();} Public String getname () {Throw new unsupportedoperationexception ();} public String getdescription () {Throw new unsupportedoperationexception ();} public double getprice () {Throw new unsupportedoperationexception ();} public Boolean isvegetarian () {Throw new unsupportedoperationexception ();} public void print () {Throw new unsupportedoperationexception ();} public iterator <menucomponent> createiterator () {Throw new unsupportedoperationexception ();}}

2. Composite iterator ).

/*** @ Time July 4, 2014 */package composite; import Java. util. iterator; import Java. util. stack;/*** @ author C. l. wang **/public class compositeiterator implements iterator <menucomponent> {stack <iterator <menucomponent> stack = new stack <iterator <menucomponent> (); /*****/Public compositeiterator (iterator <menucomponent> iterator) {// todo auto-generated constructor stubstack. push (iterator);} public bool EAN hasnext () {If (stack. empty () {return false;} else {iterator <menucomponent> iterator = (iterator <menucomponent>) stack. peek (); If (! Iterator. hasnext () {stack. pop (); Return hasnext (); // recursive call} else {return true ;}} public menucomponent next () {If (hasnext ()) {iterator <menucomponent> iterator = (iterator <menucomponent>) stack. peek (); menucomponent Component = (menucomponent) iterator. next (); If (Component instanceof menu) {stack. push (component. createiterator ();} return component;} else {return NULL;} public void remove () {Throw new unsupportedoperationexception ();}}

3. composite is used to create an iterator and composite iterator ).

/*** @ Time July 4, 2014 */package composite; import Java. util. arraylist; import Java. util. iterator;/*** @ author C. l. wang **/Public Class Menu extends menucomponent {arraylist <menucomponent> menucomponents = new arraylist <menucomponent> (); string name; string description; /*****/Public menu (string name, string description) {// todo auto-generated constructor stubthis. name = Name; this. description = description;} public void add (menucomponent) {menucomponents. add (menucomponent);} public void remove (menucomponent) {menucomponents. remove (menucomponent);} public menucomponent getchild (int I) {return (menucomponent) menucomponents. get (I) ;}public string getname () {return name;} Public String getdescription () {return description;} public void print () {system. out. print ("\ n" + getname (); system. out. println ("," + getdescription (); system. out. println ("--------------------"); iterator <menucomponent> iterator = menucomponents. iterator (); While (iterator. hasnext () {menucomponent = (menucomponent) iterator. next (); menucomponent. print () ;}} public iterator <menucomponent> createiterator () {return New compositeiterator (menucomponents. iterator ());}}

4. The leaf class (leaf) implements the method of creating an empty iterator (null iterator.

/*** @ Time July 4, 2014 */package composite; import Java. util. iterator;/*** @ author C. l. wang **/public class nulliterator implements iterator <menucomponent> {public menucomponent next () {return NULL;} public Boolean hasnext () {return false;} public void remove () {Throw new unsupportedoperationexception () ;}/ *** @ time July 4, 2014 */package composite; import Java. util. iterator;/*** @ author C. l. wang **/public class menuitem extends menucomponent {string name; string description; Boolean vegetarian; double price;/*****/Public menuitem (string name, string description, Boolean vegetarian, double price) {// todo auto-generated constructor stubthis. name = Name; this. description = description; this. vegetarian = vegetarian; this. price = price;} Public String getname () {return name;} Public String getdescription () {return description;} public double getprice () {return price;} public Boolean isvegetarian () {return vegetarian;} public void print () {system. out. print ("" + getname (); If (isvegetarian () {system. out. print ("(v)");} system. out. println ("," + getprice (); system. out. println ("--" + getdescription ();} public iterator <menucomponent> createiterator () {return New nulliterator ();}}

5. Customer class.

/*** @ Time July 4, 2014 */package composite; import Java. util. iterator;/*** @ author C. l. wang **/public class waitress {menucomponent allmenus;/*****/Public waitress (menucomponent allmenus) {// todo auto-generated constructor stubthis. allmenus = allmenus;} public void printmenu () {allmenus. print ();} public void printvegetarianmenu () {iterator <menucomponent> iterator = allmenus. createiterator (); system. out. println ("\ nvegetarian Menu \ n ----"); While (iterator. hasnext () {menucomponent = (menucomponent) iterator. next (); try {If (menucomponent. isvegetarian () menucomponent. print ();} catch (unsupportedoperationexception ex ){}}}}


6. Test class

/*** @ Time July 4, 2014 */package composite;/*** @ author C. l. wang **/public class menutestdrive {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubmenucomponent pancakehousemenu = new menu ("Pancake House menu", "Breakfast"); menucomponent dinermenu = new menu ("Diner menu ", "lunch"); menucomponent cafemenu = new menu ("cafe menu", "dinner"); menucomponent Dess Ertmenu = new menu ("dessert menu", "dessert course! "); Menucomponent coffeemenu = new menu (" coffee menu "," stuff to go with your afternoon coffee "); menucomponent allmenus = new menu (" All Menus ", "All menus combined"); allmenus. add (pancakehousemenu); allmenus. add (dinermenu); allmenus. add (cafemenu); pancakehousemenu. add (New menuitem ("K & B's pancake breakfast", "pancakes with scrambled eggs, and toast", true, 2.99); pancakehousemenu. add (New menuitem ("regular pancake breakfast", "pancakes with fried eggs, sausage", false, 2.99); pancakehousemenu. add (New menuitem ("Blueberry Pancakes", "pancakes made with fresh blueberries, and blueberry syrup", true, 3.49); pancakehousemenu. add (New menuitem ("waffles", "waffles, with your choice of blueberries or strawberries", true, 3.59); dinermenu. add (New menuitem ("vegetarian BLT", "(fakin ') bacon with lettuce & tomato on whole wheat", true, 2.99); dinermenu. add (New menuitem ("BLT", "bacon with lettuce & tomato on whole wheat", false, 2.99); dinermenu. add (New menuitem ("soup of the day", "a bowl of the soup of the day, with a side of potato salad", false, 3.29); dinermenu. add (New menuitem ("hotdog", "a hot dog, with saurkraut, relish, onions, topped with cheese", false, 3.05); dinermenu. add (New menuitem ("steamed veggies and brown rice", "steamed vegetables over brown rice", true, 3.99); dinermenu. add (New menuitem ("Pasta", "spaghetti with marinara sauce, and a slice of sourdough bread", true, 3.89); dinermenu. add (dessertmenu); dessertmenu. add (New menuitem ("apple pie", "apple pie with a flakey crust, topped with vanilla icecream", true, 1.59); dessertmenu. add (New menuitem ("Cheesecake", "creamy New York cheesecake, with a chocolate Graham crust", true, 1.99); dessertmenu. add (New menuitem ("sorbet", "a scoop of raspberry and a scoop of Lime", true, 1.89); cafemenu. add (New menuitem ("Veggie burger and air fries", "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", true, 3.99); cafemenu. add (New menuitem ("soup of the day", "a cup of the soup of the day, with a side salad", false, 3.69); cafemenu. add (New menuitem ("burrito", "a large burrito, with whole pinto beans, salsa, Guacamole", true, 4.29); cafemenu. add (coffeemenu); coffeemenu. add (New menuitem ("coffee cake", "crumbly cake topped with cinnamon and walnuts", true, 1.59); coffeemenu. add (New menuitem ("bagel", "flavors include sesame, poppyseed, cinnamon raisin, Pumpkin", false, 0.69); coffeemenu. add (New menuitem ("biscotti", "Three almond or hazelnut biscotti cookies", true, 0.89); waitress = new waitress (allmenus); waitress. printvegetarianmenu ();}}

7. output.

VEGETARIAN MENU---- K&B‘s Pancake Breakfast(v), 2.99    -- Pancakes with scrambled eggs, and toast Blueberry Pancakes(v), 3.49    -- Pancakes made with fresh blueberries, and blueberry syrup Waffles(v), 3.59    -- Waffles, with your choice of blueberries or strawberries Vegetarian BLT(v), 2.99    -- (Fakin‘) Bacon with lettuce & tomato on whole wheat Steamed Veggies and Brown Rice(v), 3.99    -- Steamed vegetables over brown rice Pasta(v), 3.89    -- Spaghetti with Marinara Sauce, and a slice of sourdough bread Apple Pie(v), 1.59    -- Apple pie with a flakey crust, topped with vanilla icecream Cheesecake(v), 1.99    -- Creamy New York cheesecake, with a chocolate graham crust Sorbet(v), 1.89    -- A scoop of raspberry and a scoop of lime Apple Pie(v), 1.59    -- Apple pie with a flakey crust, topped with vanilla icecream Cheesecake(v), 1.99    -- Creamy New York cheesecake, with a chocolate graham crust Sorbet(v), 1.89    -- A scoop of raspberry and a scoop of lime Veggie Burger and Air Fries(v), 3.99    -- Veggie burger on a whole wheat bun, lettuce, tomato, and fries Burrito(v), 4.29    -- A large burrito, with whole pinto beans, salsa, guacamole Coffee Cake(v), 1.59    -- Crumbly cake topped with cinnamon and walnuts Biscotti(v), 0.89    -- Three almond or hazelnut biscotti cookies Coffee Cake(v), 1.59    -- Crumbly cake topped with cinnamon and walnuts Biscotti(v), 0.89    -- Three almond or hazelnut biscotti cookies






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.