zoukankan      html  css  js  c++  java
  • HeadFirst设计模式之组合模式

    一、

    1.The Composite Pattern allows us to build structures of objects in the form of trees that contain both compositions of objects and individual objects as nodes.

    2.Using a composite structure,we can apply the same operations over both composites and individual objects. In other words, in most cases we can ignore the differences between compositions of objects and individual objects.

    3.The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite

    lets clients treat individual objects and compositions of objects uniformly.

    4

    .

    5.

    6.

    7.

    8.

    9.

    二、

    1.

     1 package headfirst.designpatterns.composite.menu;
     2 
     3 public abstract class MenuComponent {
     4    
     5     public void add(MenuComponent menuComponent) {
     6         throw new UnsupportedOperationException();
     7     }
     8     public void remove(MenuComponent menuComponent) {
     9         throw new UnsupportedOperationException();
    10     }
    11     public MenuComponent getChild(int i) {
    12         throw new UnsupportedOperationException();
    13     }
    14   
    15     public String getName() {
    16         throw new UnsupportedOperationException();
    17     }
    18     public String getDescription() {
    19         throw new UnsupportedOperationException();
    20     }
    21     public double getPrice() {
    22         throw new UnsupportedOperationException();
    23     }
    24     public boolean isVegetarian() {
    25         throw new UnsupportedOperationException();
    26     }
    27   
    28     public void print() {
    29         throw new UnsupportedOperationException();
    30     }
    31 }

    2.

     1 package headfirst.designpatterns.composite.menu;
     2 
     3 import java.util.Iterator;
     4 import java.util.ArrayList;
     5 
     6 public class Menu extends MenuComponent {
     7     ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
     8     String name;
     9     String description;
    10   
    11     public Menu(String name, String description) {
    12         this.name = name;
    13         this.description = description;
    14     }
    15  
    16     public void add(MenuComponent menuComponent) {
    17         menuComponents.add(menuComponent);
    18     }
    19  
    20     public void remove(MenuComponent menuComponent) {
    21         menuComponents.remove(menuComponent);
    22     }
    23  
    24     public MenuComponent getChild(int i) {
    25         return (MenuComponent)menuComponents.get(i);
    26     }
    27  
    28     public String getName() {
    29         return name;
    30     }
    31  
    32     public String getDescription() {
    33         return description;
    34     }
    35  
    36     public void print() {
    37         System.out.print("
    " + getName());
    38         System.out.println(", " + getDescription());
    39         System.out.println("---------------------");
    40   
    41         Iterator<MenuComponent> iterator = menuComponents.iterator();
    42         while (iterator.hasNext()) {
    43             MenuComponent menuComponent = 
    44                 (MenuComponent)iterator.next();
    45             menuComponent.print();
    46         }
    47     }
    48 }

    3.

     1 package headfirst.designpatterns.composite.menu;
     2 
     3 public class MenuItem extends MenuComponent {
     4     String name;
     5     String description;
     6     boolean vegetarian;
     7     double price;
     8     
     9     public MenuItem(String name, 
    10                     String description, 
    11                     boolean vegetarian, 
    12                     double price) 
    13     { 
    14         this.name = name;
    15         this.description = description;
    16         this.vegetarian = vegetarian;
    17         this.price = price;
    18     }
    19   
    20     public String getName() {
    21         return name;
    22     }
    23   
    24     public String getDescription() {
    25         return description;
    26     }
    27   
    28     public double getPrice() {
    29         return price;
    30     }
    31   
    32     public boolean isVegetarian() {
    33         return vegetarian;
    34     }
    35   
    36     public void print() {
    37         System.out.print("  " + getName());
    38         if (isVegetarian()) {
    39             System.out.print("(v)");
    40         }
    41         System.out.println(", " + getPrice());
    42         System.out.println("     -- " + getDescription());
    43     }
    44 }

    4.

     1 package headfirst.designpatterns.composite.menu;
     2   
     3 public class Waitress {
     4     MenuComponent allMenus;
     5  
     6     public Waitress(MenuComponent allMenus) {
     7         this.allMenus = allMenus;
     8     }
     9  
    10     public void printMenu() {
    11         allMenus.print();
    12     }
    13 }

    5.

      1 package headfirst.designpatterns.composite.menu; 
      2 
      3 public class MenuTestDrive {
      4     public static void main(String args[]) {
      5         MenuComponent pancakeHouseMenu = 
      6             new Menu("PANCAKE HOUSE MENU", "Breakfast");
      7         MenuComponent dinerMenu = 
      8             new Menu("DINER MENU", "Lunch");
      9         MenuComponent cafeMenu = 
     10             new Menu("CAFE MENU", "Dinner");
     11         MenuComponent dessertMenu = 
     12             new Menu("DESSERT MENU", "Dessert of course!");
     13         MenuComponent coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee");
     14   
     15         MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");
     16   
     17         allMenus.add(pancakeHouseMenu);
     18         allMenus.add(dinerMenu);
     19         allMenus.add(cafeMenu);
     20   
     21         pancakeHouseMenu.add(new MenuItem(
     22             "K&B's Pancake Breakfast", 
     23             "Pancakes with scrambled eggs, and toast", 
     24             true,
     25             2.99));
     26         pancakeHouseMenu.add(new MenuItem(
     27             "Regular Pancake Breakfast", 
     28             "Pancakes with fried eggs, sausage", 
     29             false,
     30             2.99));
     31         pancakeHouseMenu.add(new MenuItem(
     32             "Blueberry Pancakes",
     33             "Pancakes made with fresh blueberries, and blueberry syrup",
     34             true,
     35             3.49));
     36         pancakeHouseMenu.add(new MenuItem(
     37             "Waffles",
     38             "Waffles, with your choice of blueberries or strawberries",
     39             true,
     40             3.59));
     41 
     42         dinerMenu.add(new MenuItem(
     43             "Vegetarian BLT",
     44             "(Fakin') Bacon with lettuce & tomato on whole wheat", 
     45             true, 
     46             2.99));
     47         dinerMenu.add(new MenuItem(
     48             "BLT",
     49             "Bacon with lettuce & tomato on whole wheat", 
     50             false, 
     51             2.99));
     52         dinerMenu.add(new MenuItem(
     53             "Soup of the day",
     54             "A bowl of the soup of the day, with a side of potato salad", 
     55             false, 
     56             3.29));
     57         dinerMenu.add(new MenuItem(
     58             "Hotdog",
     59             "A hot dog, with saurkraut, relish, onions, topped with cheese",
     60             false, 
     61             3.05));
     62         dinerMenu.add(new MenuItem(
     63             "Steamed Veggies and Brown Rice",
     64             "Steamed vegetables over brown rice", 
     65             true, 
     66             3.99));
     67  
     68         dinerMenu.add(new MenuItem(
     69             "Pasta",
     70             "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
     71             true, 
     72             3.89));
     73    
     74         dinerMenu.add(dessertMenu);
     75   
     76         dessertMenu.add(new MenuItem(
     77             "Apple Pie",
     78             "Apple pie with a flakey crust, topped with vanilla icecream",
     79             true,
     80             1.59));
     81   
     82         dessertMenu.add(new MenuItem(
     83             "Cheesecake",
     84             "Creamy New York cheesecake, with a chocolate graham crust",
     85             true,
     86             1.99));
     87         dessertMenu.add(new MenuItem(
     88             "Sorbet",
     89             "A scoop of raspberry and a scoop of lime",
     90             true,
     91             1.89));
     92 
     93         cafeMenu.add(new MenuItem(
     94             "Veggie Burger and Air Fries",
     95             "Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
     96             true, 
     97             3.99));
     98         cafeMenu.add(new MenuItem(
     99             "Soup of the day",
    100             "A cup of the soup of the day, with a side salad",
    101             false, 
    102             3.69));
    103         cafeMenu.add(new MenuItem(
    104             "Burrito",
    105             "A large burrito, with whole pinto beans, salsa, guacamole",
    106             true, 
    107             4.29));
    108 
    109         cafeMenu.add(coffeeMenu);
    110 
    111         coffeeMenu.add(new MenuItem(
    112             "Coffee Cake",
    113             "Crumbly cake topped with cinnamon and walnuts",
    114             true,
    115             1.59));
    116         coffeeMenu.add(new MenuItem(
    117             "Bagel",
    118             "Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",
    119             false,
    120             0.69));
    121         coffeeMenu.add(new MenuItem(
    122             "Biscotti",
    123             "Three almond or hazelnut biscotti cookies",
    124             true,
    125             0.89));
    126  
    127         Waitress waitress = new Waitress(allMenus);
    128    
    129         waitress.printMenu();
    130     }
    131 }

    三、带Iterator

    1.

     1 package headfirst.designpatterns.composite.menuiterator;
     2 
     3 import java.util.*;
     4 
     5 public abstract class MenuComponent {
     6    
     7     public void add(MenuComponent menuComponent) {
     8         throw new UnsupportedOperationException();
     9     }
    10     public void remove(MenuComponent menuComponent) {
    11         throw new UnsupportedOperationException();
    12     }
    13     public MenuComponent getChild(int i) {
    14         throw new UnsupportedOperationException();
    15     }
    16   
    17     public String getName() {
    18         throw new UnsupportedOperationException();
    19     }
    20     public String getDescription() {
    21         throw new UnsupportedOperationException();
    22     }
    23     public double getPrice() {
    24         throw new UnsupportedOperationException();
    25     }
    26     public boolean isVegetarian() {
    27         throw new UnsupportedOperationException();
    28     }
    29 
    30     public abstract Iterator<MenuComponent> createIterator();
    31  
    32     public void print() {
    33         throw new UnsupportedOperationException();
    34     }
    35 }

    2.

     1 package headfirst.designpatterns.composite.menuiterator;
     2 
     3 import java.util.*;
     4   
     5 public class CompositeIterator implements Iterator<MenuComponent> {
     6     Stack<Iterator<MenuComponent>> stack = new Stack<Iterator<MenuComponent>>();
     7    
     8     public CompositeIterator(Iterator<MenuComponent> iterator) {
     9         stack.push(iterator);
    10     }
    11    
    12     public MenuComponent next() {
    13         if (hasNext()) {
    14             Iterator<MenuComponent> iterator = stack.peek();
    15             MenuComponent component = iterator.next();
    16             stack.push(component.createIterator());
    17             return component;
    18         } else {
    19             return null;
    20         }
    21     }
    22   
    23     public boolean hasNext() {
    24         if (stack.empty()) {
    25             return false;
    26         } else {
    27             Iterator<MenuComponent> iterator = stack.peek();
    28             if (!iterator.hasNext()) {
    29                 stack.pop();
    30                 return hasNext();
    31             } else {
    32                 return true;
    33             }
    34         }
    35     }
    36     
    37     /*
    38      * No longer needed as of Java 8
    39      * 
    40      * (non-Javadoc)
    41      * @see java.util.Iterator#remove()
    42      *
    43     public void remove() {
    44         throw new UnsupportedOperationException();
    45     }
    46     */
    47 }

    3.

     1 package headfirst.designpatterns.composite.menuiterator;
     2  
     3 import java.util.Iterator;
     4   
     5 public class NullIterator implements Iterator<MenuComponent> {
     6    
     7     public MenuComponent next() {
     8         return null;
     9     }
    10   
    11     public boolean hasNext() {
    12         return false;
    13     }
    14    
    15     /*
    16      * No longer needed as of Java 8
    17      * 
    18      * (non-Javadoc)
    19      * @see java.util.Iterator#remove()
    20      * 
    21     public void remove() {
    22         throw new UnsupportedOperationException();
    23     }
    24     */
    25 }

    4.

     1 package headfirst.designpatterns.composite.menuiterator;
     2 
     3 import java.util.Iterator;
     4 import java.util.ArrayList;
     5 
     6 public class Menu extends MenuComponent {
     7     Iterator<MenuComponent> iterator = null;
     8     ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
     9     String name;
    10     String description;
    11   
    12     public Menu(String name, String description) {
    13         this.name = name;
    14         this.description = description;
    15     }
    16  
    17     public void add(MenuComponent menuComponent) {
    18         menuComponents.add(menuComponent);
    19     }
    20  
    21     public void remove(MenuComponent menuComponent) {
    22         menuComponents.remove(menuComponent);
    23     }
    24  
    25     public MenuComponent getChild(int i) {
    26         return menuComponents.get(i);
    27     }
    28  
    29     public String getName() {
    30         return name;
    31     }
    32  
    33     public String getDescription() {
    34         return description;
    35     }
    36 
    37   
    38     public Iterator<MenuComponent> createIterator() {
    39         if (iterator == null) {
    40             iterator = new CompositeIterator(menuComponents.iterator());
    41         }
    42         return iterator;
    43     }
    44  
    45  
    46     public void print() {
    47         System.out.print("
    " + getName());
    48         System.out.println(", " + getDescription());
    49         System.out.println("---------------------");
    50   
    51         Iterator<MenuComponent> iterator = menuComponents.iterator();
    52         while (iterator.hasNext()) {
    53             MenuComponent menuComponent = iterator.next();
    54             menuComponent.print();
    55         }
    56     }
    57 }

    5.

     1 package headfirst.designpatterns.composite.menuiterator;
     2 
     3 import java.util.Iterator;
     4 
     5 public class MenuItem extends MenuComponent {
     6  
     7     String name;
     8     String description;
     9     boolean vegetarian;
    10     double price;
    11     
    12     public MenuItem(String name, 
    13                     String description, 
    14                     boolean vegetarian, 
    15                     double price) 
    16     { 
    17         this.name = name;
    18         this.description = description;
    19         this.vegetarian = vegetarian;
    20         this.price = price;
    21     }
    22   
    23     public String getName() {
    24         return name;
    25     }
    26   
    27     public String getDescription() {
    28         return description;
    29     }
    30   
    31     public double getPrice() {
    32         return price;
    33     }
    34   
    35     public boolean isVegetarian() {
    36         return vegetarian;
    37     }
    38 
    39     public Iterator<MenuComponent> createIterator() {
    40         return new NullIterator();
    41     }
    42  
    43     public void print() {
    44         System.out.print("  " + getName());
    45         if (isVegetarian()) {
    46             System.out.print("(v)");
    47         }
    48         System.out.println(", " + getPrice());
    49         System.out.println("     -- " + getDescription());
    50     }
    51 
    52 }

    6.

     1 package headfirst.designpatterns.composite.menuiterator;
     2 
     3 import java.util.Iterator;
     4   
     5 public class Waitress {
     6     MenuComponent allMenus;
     7  
     8     public Waitress(MenuComponent allMenus) {
     9         this.allMenus = allMenus;
    10     }
    11  
    12     public void printMenu() {
    13         allMenus.print();
    14     }
    15   
    16     public void printVegetarianMenu() {
    17         Iterator<MenuComponent> iterator = allMenus.createIterator();
    18 
    19         System.out.println("
    VEGETARIAN MENU
    ----");
    20         while (iterator.hasNext()) {
    21             MenuComponent menuComponent = iterator.next();
    22             try {
    23                 if (menuComponent.isVegetarian()) {
    24                     menuComponent.print();
    25                 }
    26             } catch (UnsupportedOperationException e) {}
    27         }
    28     }
    29 }

    7.

      1 package headfirst.designpatterns.composite.menuiterator;
      2 
      3 public class MenuTestDrive {
      4     public static void main(String args[]) {
      5 
      6         MenuComponent pancakeHouseMenu = 
      7             new Menu("PANCAKE HOUSE MENU", "Breakfast");
      8         MenuComponent dinerMenu = 
      9             new Menu("DINER MENU", "Lunch");
     10         MenuComponent cafeMenu = 
     11             new Menu("CAFE MENU", "Dinner");
     12         MenuComponent dessertMenu = 
     13             new Menu("DESSERT MENU", "Dessert of course!");
     14   
     15         MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");
     16   
     17         allMenus.add(pancakeHouseMenu);
     18         allMenus.add(dinerMenu);
     19         allMenus.add(cafeMenu);
     20   
     21         pancakeHouseMenu.add(new MenuItem(
     22             "K&B's Pancake Breakfast", 
     23             "Pancakes with scrambled eggs, and toast", 
     24             true,
     25             2.99));
     26         pancakeHouseMenu.add(new MenuItem(
     27             "Regular Pancake Breakfast", 
     28             "Pancakes with fried eggs, sausage", 
     29             false,
     30             2.99));
     31         pancakeHouseMenu.add(new MenuItem(
     32             "Blueberry Pancakes",
     33             "Pancakes made with fresh blueberries, and blueberry syrup",
     34             true,
     35             3.49));
     36         pancakeHouseMenu.add(new MenuItem(
     37             "Waffles",
     38             "Waffles, with your choice of blueberries or strawberries",
     39             true,
     40             3.59));
     41 
     42         dinerMenu.add(new MenuItem(
     43             "Vegetarian BLT",
     44             "(Fakin') Bacon with lettuce & tomato on whole wheat", 
     45             true, 
     46             2.99));
     47         dinerMenu.add(new MenuItem(
     48             "BLT",
     49             "Bacon with lettuce & tomato on whole wheat", 
     50             false, 
     51             2.99));
     52         dinerMenu.add(new MenuItem(
     53             "Soup of the day",
     54             "A bowl of the soup of the day, with a side of potato salad", 
     55             false, 
     56             3.29));
     57         dinerMenu.add(new MenuItem(
     58             "Hotdog",
     59             "A hot dog, with saurkraut, relish, onions, topped with cheese",
     60             false, 
     61             3.05));
     62         dinerMenu.add(new MenuItem(
     63             "Steamed Veggies and Brown Rice",
     64             "A medly of steamed vegetables over brown rice", 
     65             true, 
     66             3.99));
     67  
     68         dinerMenu.add(new MenuItem(
     69             "Pasta",
     70             "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
     71             true, 
     72             3.89));
     73    
     74         dinerMenu.add(dessertMenu);
     75   
     76         dessertMenu.add(new MenuItem(
     77             "Apple Pie",
     78             "Apple pie with a flakey crust, topped with vanilla icecream",
     79             true,
     80             1.59));
     81         dessertMenu.add(new MenuItem(
     82             "Cheesecake",
     83             "Creamy New York cheesecake, with a chocolate graham crust",
     84             true,
     85             1.99));
     86         dessertMenu.add(new MenuItem(
     87             "Sorbet",
     88             "A scoop of raspberry and a scoop of lime",
     89             true,
     90             1.89));
     91 
     92         cafeMenu.add(new MenuItem(
     93             "Veggie Burger and Air Fries",
     94             "Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
     95             true, 
     96             3.99));
     97         cafeMenu.add(new MenuItem(
     98             "Soup of the day",
     99             "A cup of the soup of the day, with a side salad",
    100             false, 
    101             3.69));
    102         cafeMenu.add(new MenuItem(
    103             "Burrito",
    104             "A large burrito, with whole pinto beans, salsa, guacamole",
    105             true, 
    106             4.29));
    107  
    108         Waitress waitress = new Waitress(allMenus);
    109    
    110         waitress.printVegetarianMenu();
    111  
    112     }
    113 }
  • 相关阅读:
    nno_setup制作升级包必须面临的几个问题 2
    如何运用inno在安装和卸载时提示用户结束进程?
    inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效
    Inno Setup 插件大全
    INNO安装卸载自动结束进程插件使用
    Inno Setup 在安装程序开始前和卸载程序开始前,检查并关闭运行的进程
    INNO:检测程序是否已经安装,是则弹出卸载提示。
    Inno Setup 安装、卸载前检测进程或服务
    断点续传2
    解决 IDEA 创建 Gradle 项目没有src目录问题
  • 原文地址:https://www.cnblogs.com/shamgod/p/5261461.html
Copyright © 2011-2022 走看看