设计模式学习专栏十--------组合模式

场景


回顾迭代器模式的案例

我们希望能够加上一份 餐后甜点的 "子菜单"

我们需要什么?

  • 我们需要某种树形结构 , 可以容纳菜单 , 子菜单和菜单项
  • 我们需要确定能够在每个菜单的各个项之间游走 . 而且至少要像现在用迭代器一样方便
  • 我们也需要能够更有弹性地在菜单项之间游走 . 比如说 , 可以只需要遍历甜品菜单, 或者可以遍历餐厅的整个菜单(包括甜点菜单在内).

如何实现呢?

案例结构图

案例类图

组合模式总览


定义:允许你将对象组成树形结构来表现"整体 / 部分"的层次结构.组合能让客户以一致的方式处理个别对象和对象组合(忽略整体/部分的区别)
我们可以使用这个模式来 "统一处理个别对象 和 组合对象"
  • 模式的理解

    • 类图

    • 角色

      • 叶子结点Leaf
      • 组合结点 Composite
      • 叶子结点与组合结点共同接口 Component
    • 细节

      • 当我们有数个对象的集合, 它们之间有"整体/部分"的关系,并且我们想用一致的方法对待这些对象时,就可以使用组合模式
      • 组合模式提供了一个结构,可同时包容个别对象和组合对象
      • 组合结构内的任意对象为组件 , 组件可以是组合,也可以是叶子结点
      • 如果有些对象具备一些没有意义的调用(比如叶子结点的getChild), 我们可以返回null值或者false

        我们可以把没意义的调用放到父类中去默认实现 , 但这样会失去一些"安全性" (调用无效操作),

        或者把责任区分别放到不同的接口中 (比如把add , getChild放到叶子结点) , 但那样子对客户就失去了"透明性"(需要使用instanceof判断类型并强转) , 因此这需要我们折衷

案例代码部分

  • 抽象组件MenuComponent

    public abstract class MenuComponent {
       
        public void add(MenuComponent menuComponent) {
            throw new UnsupportedOperationException();
        }
        public void remove(MenuComponent 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 abstract Iterator<MenuComponent> createIterator();
     
        public void print() {
            throw new UnsupportedOperationException();
        }
    }
  • 菜单项MenuItem

    public class MenuItem extends MenuComponent {
     
        String name;
        String description;
        boolean vegetarian;
        double price;
        
        public MenuItem(String name, 
                        String description, 
                        boolean vegetarian, 
                        double price) 
        { 
            this.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 Iterator<MenuComponent> createIterator() {
            return new NullIterator();
        }
     
        public void print() {
            System.out.print("  " + getName());
            if (isVegetarian()) {
                System.out.print("(v)");
            }
            System.out.println(", " + getPrice());
            System.out.println("     -- " + getDescription());
        }
    
    }
  • 菜单Menu

    public class Menu extends MenuComponent {
        Iterator<MenuComponent> iterator = null;
        ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
        String name;
        String description;
      
        public Menu(String name, String description) {
            this.name = name;
            this.description = description;
        }
         //菜单Menu重写 add , remove ,getChild方法
        public void add(MenuComponent menuComponent) {
            menuComponents.add(menuComponent);
        }
     
        public void remove(MenuComponent menuComponent) {
            menuComponents.remove(menuComponent);
        }
     
        public MenuComponent getChild(int i) {
            return menuComponents.get(i);
        }
     
        public String getName() {
            return name;
        }
     
        public String getDescription() {
            return description;
        }
    
      
        public Iterator<MenuComponent> createIterator() {
            if (iterator == null) {
                iterator = new CompositeIterator(menuComponents.iterator());
            }
            return iterator;
        }
     
     
        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 class MenuTestDrive {
        public static void main(String args[]) {
    
            MenuComponent pancakeHouseMenu = 
                new Menu("PANCAKE HOUSE MENU", "Breakfast");
            MenuComponent dinerMenu = 
                new Menu("DINER MENU", "Lunch");
      
            MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");
      
            allMenus.add(pancakeHouseMenu);
            allMenus.add(dinerMenu);
      
            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",
                "A medly of 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));
    
            Waitress waitress = new Waitress(allMenus);
            waitress.printMenu();
        }
    }
  • 输出结果

    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 strawberries
    
    DINER 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
         -- A medly of steamed vegetables over brown rice
      Pasta(v), 3.89
         -- Spaghetti with Marinara Sauce, and a slice of sourdough bread

参考

​ 书籍: HeadFirst设计模式

​ 代码参考地址: 我就是那个地址


你的头发真的好长
42 声望5 粉丝