{"id":1082,"date":"2024-06-06T17:01:01","date_gmt":"2024-06-06T09:01:01","guid":{"rendered":"http:\/\/www.spiceandwolf.com.cn\/?p=1082"},"modified":"2024-06-06T17:01:02","modified_gmt":"2024-06-06T09:01:02","slug":"design-pattern-composite-pattern","status":"publish","type":"post","link":"http:\/\/www.spiceandwolf.com.cn\/index.php\/design-pattern-composite-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f-\u7ec4\u5408\u6a21\u5f0f"},"content":{"rendered":"\n<p><strong>\u7ec4\u5408\u6a21\u5f0f<\/strong>\u5141\u8bb8\u4f60\u5c06\u5bf9\u8c61\u7ec4\u5408\u6210\u6811\u5f62\u7ed3\u6784\u6765\u8868\u73b0&#8221;\u6574\u4f53\/\u90e8\u5206&#8221;\u5c42\u6b21\u7ed3\u6784\u3002\u7ec4\u5408\u80fd\u8ba9\u5ba2\u6237\u4ee5\u4e00\u81f4\u7684\u65b9\u5f0f\u5904\u7406\u4e2a\u522b\u5bf9\u8c61\u4ee5\u53ca\u5bf9\u8c61\u7ec4\u5408\u3002<\/p>\n\n\n\n<p>\u4e66\u63a5\u4e0a\u6587\uff0c\u83dc\u5355\u7c7b\u7684\u5b9e\u73b0\u5e76\u4e0d\u80fd\u901a\u8fc7\u8fed\u4ee3\u5668\u6a21\u5f0f\u8fbe\u5230\u5b8c\u5168\u89e3\u8026\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u7ec4\u5408\u6a21\u5f0f\u505a\u8fdb\u4e00\u6b65\u7684\u4f18\u5316\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u8ba9menu\u548cmenuItem\u90fd\u7ee7\u627fMenuComponent\u7c7b\uff0c\u4ece\u800c\u8ba9\u83dc\u5355\u548c\u83dc\u5355\u9879\u5177\u6709\u76f8\u540c\u7684\u7236\u7c7b\uff0c\u5171\u540c\u4f5c\u4e3a\u6811\u7ed3\u6784\u7684\u8282\u70b9\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u5728\u83dc\u5355\u7c7b\u4e2d\u5c06\u7236\u7c7b\u4e2d\u4e0d\u5177\u6709\u7684\u884c\u4e3a\u7528\u5f02\u5e38\u66ff\u4ee3\u3002\u5e76\u5728\u904d\u5386\u5230\u83dc\u5355\u9879\u65f6\u8fd4\u56de\u7a7a\u8fed\u4ee3\u5668\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public abstract class MenuComponent {\n    public void add(MenuComponent menuComponent) {\n        throw new UnsupportedOperationException();\n    }\n\n    public void remove(MenuComponent menuComponent) {\n        throw new UnsupportedOperationException();\n    }\n\n    public MenuComponent getChild(int i) {\n        throw new UnsupportedOperationException();\n    }\n\n    public String getName() {\n        throw new UnsupportedOperationException();\n    }\n\n    public String getDescription() {\n        throw new UnsupportedOperationException();\n    }\n\n    public double getPrice() {\n        throw new UnsupportedOperationException();\n    }\n\n    public boolean isVegetarian() {\n        throw new UnsupportedOperationException();\n    }\n\n    public void print() {\n        throw new UnsupportedOperationException();\n    }\n\n    public Iterator createIterator() {\n        return new NullIterator();\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Menu extends MenuComponent {\n    List&lt;MenuComponent> menuComponents = new ArrayList&lt;>();\n    String name;\n    String description;\n\n    public Menu(String name,\n                String description) {\n        this.name = name;\n        this.description = description;\n    }\n\n    @Override\n    public String getName() {\n        return name;\n    }\n\n    @Override\n    public String getDescription() {\n        return description;\n    }\n\n    public void add(MenuComponent menuComponent) {\n        menuComponents.add(menuComponent);\n    }\n\n    public void remove(MenuComponent menuComponent) {\n        menuComponents.remove(menuComponent);\n    }\n\n    public MenuComponent getChild(int i) {\n        return menuComponents.get(i);\n    }\n\n    public void print() {\n        System.out.println(\"\\n\" + getName());\n        System.out.println(\", \" + getDescription());\n        System.out.println(\"----------------------\");\n\n        Iterator iterator = menuComponents.iterator();\n        while(iterator.hasNext()) {\n            MenuComponent menuComponent = (MenuComponent) iterator.next();\n            menuComponent.print();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MenuItem extends MenuComponent {\n    String name;\n    String description;\n    boolean vegetarian;\n    double price;\n\n    public MenuItem(String name, String description, boolean vegetarian, double price) {\n        this.name = name;\n        this.description = description;\n        this.vegetarian = vegetarian;\n        this.price = price;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public String getDescription() {\n        return description;\n    }\n\n    public double getPrice() {\n        return price;\n    }\n\n    public boolean isVegetarian() {\n        return vegetarian;\n    }\n\n    public void print() {\n        System.out.println(\" \" + getName());\n        if (isVegetarian()) {\n            System.out.println(\"(v)\");\n        }\n        System.out.println(\", \" + getPrice());\n        System.out.println(\"  -- \" + getDescription());\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Waitress {\n    MenuComponent allMenus;\n\n    public Waitress(MenuComponent allMenus) {\n        this.allMenus = allMenus;\n    }\n\n    public void printMenu() {\n        allMenus.print();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u5b8c\u5168\u89e3\u8026\u4e0d\u540c\u7684\u96c6\u5408\u5b9e\u73b0\u6765\u8fed\u4ee3\u904d\u5386\u83dc\u5355\u9879\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MenuTestDrive {\n    public static void main(String&#91;] args) {\n        MenuComponent pancakeHouseMenu = new Menu(\"PANCAKE HOUSE MENU\", \"Breakfast\");\n        MenuComponent dinerMenu = new Menu(\"DINER MENU\", \"LUNCH\");\n        MenuComponent cafeMenu = new Menu(\"CAFE MENU\", \"Dinner\");\n        MenuComponent dessertMenu = new Menu(\"DESSERT MENU\", \"Dessert of course\");\n        MenuComponent coffeeMenu = new Menu(\"COFFEE MENU\",\"Stuff to go with your afternoon coffee\");\n\n        MenuComponent allMenus = new Menu(\"ALL MENUS\", \"ALL menus combinded\");\n\n        allMenus.add(pancakeHouseMenu);\n        allMenus.add(dinerMenu);\n        allMenus.add(cafeMenu);\n\n        pancakeHouseMenu.add(new MenuItem(\n                \"K&amp;B's Pancake Breakfast\",\n                \"Pancakes with scrambled eggs and toast\",\n                true,\n                2.99));\n        pancakeHouseMenu.add(new MenuItem(\n                \"Regular Pancake Breakfast\",\n                \"Pancakes with fried eggs, sausage\",\n                false,\n                2.99));\n        pancakeHouseMenu.add(new MenuItem(\n                \"Blueberry Pancakes\",\n                \"Pancakes made with fresh blueberries, and blueberry syrup\",\n                true,\n                3.49));\n        pancakeHouseMenu.add(new MenuItem(\n                \"Waffles\",\n                \"Waffles with your choice of blueberries or strawberries\",\n                true,\n                3.59));\n\n        dinerMenu.add(new MenuItem(\n                \"Vegetarian BLT\",\n                \"(Fakin') Bacon with lettuce &amp; tomato on whole wheat\",\n                true,\n                2.99));\n        dinerMenu.add(new MenuItem(\n                \"BLT\",\n                \"Bacon with lettuce &amp; tomato on whole wheat\",\n                false,\n                2.99));\n        dinerMenu.add(new MenuItem(\n                \"Soup of the day\",\n                \"A bowl of the soup of the day, with a side of potato salad\",\n                false,\n                3.29));\n        dinerMenu.add(new MenuItem(\n                \"Hot Dog\",\n                \"A hot dog, with saurkraut, relish, onions, topped with cheese\",\n                false,\n                3.05));\n        dinerMenu.add(new MenuItem(\n                \"Steamed Veggies and Brown Rice\",\n                \"Steamed vegetables over brown rice\",\n                true,\n                3.99));\n\n        dinerMenu.add(new MenuItem(\n                \"Pasta\",\n                \"Spaghetti with marinara sauce, and a slice of sourdough bread\",\n                true,\n                3.89));\n\n        dinerMenu.add(dessertMenu);\n\n        dessertMenu.add(new MenuItem(\n                \"Apple Pie\",\n                \"Apple pie with a flakey crust, topped with vanilla icecream\",\n                true,\n                1.59));\n\n        dessertMenu.add(new MenuItem(\n                \"Cheesecake\",\n                \"Creamy New York cheesecake, with a chocolate graham crust\",\n                true,\n                1.99));\n        dessertMenu.add(new MenuItem(\n                \"Sorbet\",\n                \"A scoop of raspberry and a scoop of lime\",\n                true,\n                1.89));\n\n        cafeMenu.add(new MenuItem(\n                \"Veggie Burger and Air Fries\",\n                \"Veggie burger on a whole wheat bun, lettuce, tomato, and fries\",\n                true,\n                3.99));\n        cafeMenu.add(new MenuItem(\n                \"Soup of the day\",\n                \"A cup of the soup of the day, with a side salad\",\n                false,\n                3.69));\n        cafeMenu.add(new MenuItem(\n                \"Burrito\",\n                \"A large burrito, with whole pinto beans, salsa, guacamole\",\n                true,\n                4.29));\n\n        cafeMenu.add(coffeeMenu);\n\n        coffeeMenu.add(new MenuItem(\n                \"Coffee Cake\",\n                \"Crumbly cake topped with cinnamon and walnuts\",\n                true,\n                1.59));\n        coffeeMenu.add(new MenuItem(\n                \"Bagel\",\n                \"Flavors include sesame, poppyseed, cinnamon raisin, pumpkin\",\n                false,\n                0.69));\n        coffeeMenu.add(new MenuItem(\n                \"Biscotti\",\n                \"Three almond or hazelnut biscotti cookies\",\n                true,\n                0.89));\n\n        Waitress waitress = new Waitress(allMenus);\n        \n        waitress.printMenu();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec4\u5408\u6a21\u5f0f\u5141\u8bb8\u4f60\u5c06\u5bf9\u8c61\u7ec4\u5408\u6210\u6811\u5f62\u7ed3\u6784\u6765\u8868\u73b0&#8221;\u6574\u4f53\/\u90e8\u5206&#8221;\u5c42\u6b21\u7ed3\u6784\u3002\u7ec4\u5408\u80fd\u8ba9\u5ba2\u6237\u4ee5\u4e00\u81f4\u7684\u65b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-1082","post","type-post","status-publish","format-standard","hentry","category-design-pattern"],"_links":{"self":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1082"}],"version-history":[{"count":2,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1082\/revisions"}],"predecessor-version":[{"id":1084,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1082\/revisions\/1084"}],"wp:attachment":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1082"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}