Detailed description of Design Pattern-composite pattern

Source: Internet
Author: User
Composite pattern


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


Combination Mode: allows you to combine objects into a tree structure to express the "Overall/partial" hierarchy. combination allows you to process individual objects and composite objects in a consistent way.


CreateComponent class and Composite)AndLeaf (leaf)Inherit component classes,Client)Directly call the top-level composite class.


Specific Method:

1.Component class)Contains the methods used by composite and leaf, that is, a node of the tree structure. abstract is used ).

If not definedException).

/*** @ Time July 4, 2014 */package composite;/*** @ 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 ();}}

2. Leaf (leaf)Inherits component classes to implement specific output methods.

/*** @ Time July 4, 2014 */package composite;/*** @ 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 ());}}

3. Composite)Inherit component classes and implement the required methods. You can add some leaf classes (leaf) as subnodes.

/*** @ 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 ();}}}

4. Client)You can directly call the composite.

/*** @ Time July 4, 2014 */package composite;/*** @ 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 ();}}

5. test:

/*** @ 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. printmenu ();}}


6. Output:

ALL MENUS, All menus combined--------------------PANCAKE HOUSE MENU, Breakfast-------------------- K&B‘s Pancake Breakfast(v), 2.99    -- Pancakes with scrambled eggs, and toast Regular Pancake Breakfast, 2.99    -- Pancakes with fried eggs, sausage Blueberry Pancakes(v), 3.49    -- Pancakes made with fresh blueberries, and blueberry syrup Waffles(v), 3.59    -- Waffles, with your choice of blueberries or strawberriesDINER MENU, Lunch-------------------- Vegetarian BLT(v), 2.99    -- (Fakin‘) Bacon with lettuce & tomato on whole wheat BLT, 2.99    -- Bacon with lettuce & tomato on whole wheat Soup of the day, 3.29    -- A bowl of the soup of the day, with a side of potato salad Hotdog, 3.05    -- A hot dog, with saurkraut, relish, onions, topped with cheese 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 breadDESSERT MENU, Dessert course!-------------------- 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 limeCAFE MENU, Dinner-------------------- Veggie Burger and Air Fries(v), 3.99    -- Veggie burger on a whole wheat bun, lettuce, tomato, and fries Soup of the day, 3.69    -- A cup of the soup of the day, with a side salad Burrito(v), 4.29    -- A large burrito, with whole pinto beans, salsa, guacamoleCOFFEE MENU, Stuff to go with your afternoon coffee-------------------- Coffee Cake(v), 1.59    -- Crumbly cake topped with cinnamon and walnuts Bagel, 0.69    -- Flavors include sesame, poppyseed, cinnamon raisin, pumpkin 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.