设计模式之迭代器与组合模式(四)

因为这系列篇幅较长,所以在这里也不进行任何铺垫,直奔主题去啦。

利用组合设计菜单

我们要如何在菜单上应用组合模式呢?一开始,我们需要创建一个组件接口来作为菜单和菜单项的共同接口,让我们能够用统一的做法来处理菜单和菜单项。换句话说,我们可以针对菜单或菜单项调用相同的方法。

让我们从头来看看如何让菜单能够符合组合模式的结构:

实现菜单组件

好了,我们开始编写菜单组件的抽象类;请记住,菜单组件的角色是为叶节点和组合节点提供一个共同的接口。

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

让我们来看菜单类。别忘了,这是组合类图里的叶类,它实现组合内元素的行为。

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

}

我们已经有了菜单项,还需要组合类,这就是我们叫做菜单的。别忘了,此组合类可以持有菜单项或其他菜单。

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;
    }

    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("---------------------");
    }
}

因为菜单是一个组合,包含了菜单项和其他的菜单,所以它的print()应该打印出它所包含的一切。如果它不这么做,我们就必须遍历整个组合的每个节点,然后将每一项打印出来。这么一来,也就失去了使用组合结构的意义

所以,print还得进行优化,如下:

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

看到上面了没,我们用了迭代器。用它遍历所有菜单组件,遍历过程中,可能遇到其他菜单,或者是遇到菜单项。由于菜单和菜单项都实现了print,那我们只要调用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 cafeMenu =
            new Menu("CAFE MENU", "Dinner");
        MenuComponent dessertMenu =
            new Menu("DESSERT MENU", "Dessert of course!");

        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",
            "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));

        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));

        Waitress waitress = new Waitress(allMenus);

        waitress.printVegetarianMenu();

    }
}

结果这里就不附上了,请大家自行去跑代码实现吧。相信你们又对组合模式也已经有了一个大概了吧。下一篇,还有更犀利的,组合迭代器等着我们。小编马上回去搞起来,安排上。

爱生活,爱学习,爱感悟,爱挨踢

原文地址:https://www.cnblogs.com/dimple91/p/11089204.html

时间: 2024-08-04 13:14:07

设计模式之迭代器与组合模式(四)的相关文章

设计模式 3 —— 迭代器和组合模式

设计模式目录: 设计模式 1 ——观察者模式 设计模式 2 —— 装饰者模式 设计模式 3 —— 迭代器和组合模式 概要 设计模式 3 -- 迭代器和组合模式

设计模式(9)--迭代器与组合模式

(1)散列表(hashtable)         共迭代器获取 hashtable.values().iterator();  因为每一笔数据都是由键值对组成. (2)迭代器是用来遍历集合的. Java5后集合都出了自己的遍历方式 如增加for循环.           遍历 也称 游走... (3)数组:Array 长定固定   集合:ArrayList 可扩展,取数据不需要转型 (4)Java中有自己的迭代器接口.在java.util.Iterator  . (5)集合:collectio

设计模式(9.1)--迭代器与组合模式

缓存(caching): 当组合结构很复杂,或者遍历的代价太高,那么实现组合节点的缓存就很有帮助. (1) Java Collection Framework 指的是一群类的接口. 其中包括了 ArrayList, Vector , LinkedList , Stack ,和 PriorityQueue. 这些类都实现了java.util.Collection接口. (2) Collection接口中的一些方法: add()  , addAll()  , clear() , contains()

设计模式(七)组合模式Composite(结构型)

设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 我们可以使用简单的对象组合成复杂的对象,而这个复杂对象有可以组合成更大的对象.我们可以把简单这些对象定义成类,然后定义一些容器类来存储这些简单对象.客户端代码必须区别对象简单对象和容器对象,而实际上大多数情况下用户认为它们是一样的.对这些类区别使用,使得程序更加复杂.递归使用的时候跟麻烦,而我们如何

迭代器与组合模式

迭代模式与组合模式要点: 1.迭代器允许访问聚合的元素,而不需要暴露它的内部结构 2.迭代器将遍历聚合的工作封装进一个对象中 3.当使用迭代器的时候,我们一来聚合提供遍历 4.迭代器提供了一个通用的接口,让我们遍历聚合的项时,就可以使用多态机制 5.我们应该努力让一个类只分配一个责任 6.组合模式提供一个结构,可同时包容个别对象和组合对象 7.组合模式允许客户对个别对象以及组合对象一视同仁 8.组合结构内的任意对象称为组件,组件可以使组合,也可以是叶子节点 9.在实现组合模式时,有许多设计上的折

设计模式08: Composite 组合模式(结构型模式)

Composite 组合模式(结构型模式) 对象容器的问题在面向对象系统中,我们常会遇到一类具有“容器”特征的对象——即他们在充当对象的同时,又是其他对象的容器. public interface IBox { void Process(); } public class SingleBox:IBox { public void Process(){...} } public class ContainerBox:IBox { public void Process(){...} public

headFirst学习笔记之九:迭代器与组合模式(5.1)

1.任务: 大新闻!对象村餐厅和对象村煎饼屋合并了!可以在同一个地方吃早饭和午饭了hohoho(有什么好开森的对象村的小伙伴们好容易满足). 但是有一个问题出现了:煎饼屋的菜单menu是用ArrayList记录菜单项menuItem,但是餐厅的菜单menu使用数组Array记录menuItem.大家都不愿意修改自己的结构,那么java女招待的任务就很繁重了啊,因为取出菜单项的方法就要记住两种了:menuItems.get(i)和menuItems[i]. 1 public class MenuI

迭代器和组合模式

一.迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 类图关系:            二.组合模式:允许你将对象组合成树形结构来表现"整体/部分"层次结构,组合能让客户以一致处理的方式处理个别对象以及对象的组合. 组合模式让我们能用树形方式创建对象的结构,树里面包含了组合和个别对象,使用组合我们应用相同的操作在组合和个别对象身上.及大多数情况下,可忽略组合和个别对象的差距. 类图:             

设计模式学习笔记之组合模式

组合模式 允许你将对象组合成树形结构来表现“整体/部分”层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 使用组合结构,我们能把相同的操作应用在组合和个别对象上.换句话说,在大多数情况下,我们可以忽略对象组合和个别对象之间的差别. 说明: 1.组合模式使添加或者删除子节点变得容易: 场景: 1.想要标识对象的部分-整体结构: 2.想要客户端忽略组合对象和个体对象的差异,客户端将统一使用组合结构中的所有对象: /** * 为组合中所有对象定义一个接口,不管是组合还是节点 * */ pu