zoukankan      html  css  js  c++  java
  • 设计模式 3 —— 迭代器和组合模式(迭代器)

    设计模式目录:

    设计模式 1 ——观察者模式

    设计模式 2 —— 装饰者模式 

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

    设计模式 4 —— 迭代器和组合模式(组合)

    设计模式 5 —— 工厂模式

    设计模式 6 —— 单件模式

    设计模式 7 —— 命令模式

    设计模式 8 —— 适配器和外观模式

    设计模式 9 —— 模板方法模式

    设计模式 10 —— 状态模式

     

    概要

    第1部分 问题引入

    第2部分 迭代器模式定义

    第1部分 问题引入

      餐厅和煎饼屋要合并,要把煎饼屋的菜单作为早餐菜单,餐厅的菜单作为午餐菜单。但是对于菜单项的记录,前者用的是ArrayList,后者用的是数组,两者都不愿意改变代码实现。所以在女招待处理的时候,需要用不同的方法分别处理这两个菜单,毕竟菜单项的返回值一个是ArrayList一个是数组,遍历的时候也要分别遍历。

      之前的学习中一直说要封装变化的部分,但是由不同的集合类型所造成的遍历也可以封装吗?迭代器模式就是解决这个问题。

    首先看下菜单项的代码:

    菜单项:

     1 package firsthead.interater;
     2 
     3 /**
     4  * @ClassName: MenuItem
     5  * @Description: 菜单项
     6  * @author xingle
     7  * @date 2014年7月31日 下午9:51:11
     8  */
     9 public class MenuItem {
    10     String name;
    11     String description;
    12     //是否为素食
    13     boolean vegetarian;
    14     double price;
    15 
    16     public MenuItem(String name, String description, boolean vegetarian,
    17             double price) {
    18         this.name = name;
    19         this.description = description;
    20         this.vegetarian = vegetarian;
    21         this.price = price;
    22     }
    23 
    24     public String getName() {
    25         return name;
    26     }
    27 
    28     public String getDescription() {
    29         return description;
    30     }
    31 
    32     public double getPrice() {
    33         return price;
    34     }
    35 
    36     public boolean isVegetarian() {
    37         return vegetarian;
    38     }
    39 
    40     public String toString() {
    41         return (name + ", $" + price + "
       " + description);
    42     }
    43 
    44 }

    下面是煎饼屋和餐厅的菜单:

     1 package firsthead.interater;
     2 
     3 import java.util.ArrayList;
     4 
     5 /**
     6  * @ClassName: PancakeHouseMenu
     7  * @Description: 煎饼屋菜单
     8  * @author xingle
     9  * @date 2014年7月31日 下午10:09:33
    10  */
    11 public class PancakeHouseMenu {
    12 
    13     ArrayList menuItems;
    14 
    15     public PancakeHouseMenu() {
    16         // 使用ArrayList存储菜单项
    17         menuItems = new ArrayList();
    18 
    19         addItem("K&B's Pancake Breakfast",
    20                 "Pancakes with scrambled eggs, and toast", true, 2.99);
    21 
    22         addItem("Regular Pancake Breakfast",
    23                 "Pancakes with fried eggs, sausage", false, 2.99);
    24 
    25         addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries",
    26                 true, 3.49);
    27 
    28         addItem("Waffles",
    29                 "Waffles, with your choice of blueberries or strawberries",
    30                 true, 3.59);
    31 
    32     }
    33 
    34     // 加入一个菜单项:创建一个新的菜单项对象,加入ArrayList
    35     public void addItem(String name, String description, boolean vegetarian,
    36             double price) {
    37         MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
    38         menuItems.add(menuItem);
    39     }
    40     
    41     //返回菜单项列表
    42     public ArrayList getMenuItems(){
    43         return menuItems;
    44     }
    45     
    46     public Iterator createIterator(){
    47         return new PancakeHouseMenuIterator(menuItems);
    48     }
    49     //其他方法。。。。
    50     
    51 
    52 }
     1 package firsthead.interater;
     2 
     3 /**
     4  * @ClassName: DinerMenu
     5  * @Description: 餐厅菜单
     6  * @author xingle
     7  * @date 2014年7月31日 下午10:18:17
     8  */
     9 public class DinerMenu {
    10 
    11     static final int MAX_ITEMS = 6;
    12     int numberOfItems = 0;
    13     // 使用数组形式
    14     MenuItem[] menuItems;
    15 
    16     public DinerMenu() {
    17         menuItems = new MenuItem[MAX_ITEMS];
    18 
    19         addItem("Vegetarian BLT",
    20                 "(Fakin') Bacon with lettuce & tomato on whole wheat", true,
    21                 2.99);
    22         addItem("BLT", "Bacon with lettuce & tomato on whole wheat", false,
    23                 2.99);
    24         addItem("Soup of the day",
    25                 "Soup of the day, with a side of potato salad", false, 3.29);
    26         addItem("Hotdog",
    27                 "A hot dog, with saurkraut, relish, onions, topped with cheese",
    28                 false, 3.05);
    29         addItem("Steamed Veggies and Brown Rice",
    30                 "Steamed vegetables over brown rice", true, 3.99);
    31         addItem("Pasta",
    32                 "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
    33                 true, 3.89);
    34     }
    35 
    36     public void addItem(String name, String description, boolean vegetarian,
    37             double price) {
    38         MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
    39         if (numberOfItems >= MAX_ITEMS) {
    40             System.err.println("Sorry, menu is full!  Can't add item to menu");
    41         } else {
    42             menuItems[numberOfItems] = menuItem;
    43             numberOfItems = numberOfItems + 1;
    44         }
    45     }
    46 
    47     public MenuItem[] getMenuItems() {
    48         return menuItems;
    49     }
    50     
    51     public Iterator createIterator(){
    52         return new DinerMenuIterator(menuItems);
    53     }
    54 
    55     // 其他的方法
    56 }

    为了封装打印菜单的遍历,上面代码分别新建了一个菜单的迭代器createIterator(), 其中PancakeHouseMenuIterator(),和DinerMenuIterator()的迭代器代码如下:

     1 package firsthead.interater;
     2 
     3 import java.util.ArrayList;
     4 
     5 /**
     6  * @ClassName: PancakeHouseMenuIterator
     7  * @Description: 实现一个具体的迭代器,煎饼屋菜单迭代器
     8  * @author xingle
     9  * @date 2014年8月1日 下午2:12:37
    10  */
    11 public class PancakeHouseMenuIterator implements Iterator{
    12     
    13     ArrayList items;
    14     int position =0;
    15     
    16     public PancakeHouseMenuIterator(ArrayList items){
    17         this.items = items;
    18     }
    19 
    20     /* 
    21     * Title: hasNext
    22     * Description: 
    23     * @return 
    24     * @see firsthead.interater.Iterator#hasNext() 
    25     */
    26     @Override
    27     public boolean hasNext() {
    28         if (position >= items.size()) {
    29             return false;
    30         } else {
    31             return true;
    32         }
    33     }
    34 
    35     /* 
    36     * Title: next
    37     * Description: 
    38     * @return 
    39     * @see firsthead.interater.Iterator#next() 
    40     */
    41     @Override
    42     public Object next() {
    43         Object object = items.get(position);
    44         position = position + 1;
    45         return object;
    46     }
    47 
    48 }
     1 package firsthead.interater;
     2 
     3 /**
     4  * @ClassName: DinerMenuIterator
     5  * @Description: 实现一个具体的迭代器,餐厅菜单迭代器
     6  * @author xingle
     7  * @date 2014年8月1日 下午1:56:06
     8  */
     9 public class DinerMenuIterator implements Iterator{
    10     
    11     MenuItem[] items;
    12     //记录当前数组遍历的位置
    13     int position = 0;
    14     
    15     //构造器需要传入一个菜单项的数组当做参数
    16     public DinerMenuIterator(MenuItem[] items){
    17         this.items = items;
    18     }
    19 
    20     /* 
    21     * Title: hasNext
    22     * Description: 
    23     * @return 
    24     * @see firsthead.interater.Iterator#hasNext() 
    25     */
    26     @Override
    27     public boolean hasNext() {
    28         if (position >= items.length || items[position] == null) {
    29             return false;
    30         } else {
    31             return true;
    32         }
    33     }
    34 
    35     /* 
    36     * Title: next
    37     * Description: 返回数组下一项并递增其位置
    38     * @return 
    39     * @see firsthead.interater.Iterator#next() 
    40     */
    41     @Override
    42     public Object next() {
    43         MenuItem menuItem = items[position];
    44         position = position + 1;
    45         return menuItem;
    46     }
    47 
    48 }

    以上两个迭代器都继承同一个迭代器Iterator,这里我们自己创建该迭代器:

     1 package firsthead.interater;
     2 
     3 /**
     4  * @ClassName: Iterator
     5  * @Description: 自定义迭代器
     6  * @author xingle
     7  * @date 2014年8月1日 下午1:54:56
     8  */
     9 public interface Iterator {
    10     boolean hasNext();
    11     Object next();
    12 }

    下面是女招待的代码:

     1 package firsthead.interater;
     2 
     3 /**
     4  * @ClassName: Waitress
     5  * @Description: 女招待的代码
     6  * @author xingle
     7  * @date 2014年8月1日 下午2:19:46
     8  */
     9 public class Waitress {
    10     //煎饼屋菜单
    11     PancakeHouseMenu pancakeHouseMenu;
    12     //餐厅菜单
    13     DinerMenu dinerMenu;
    14     
    15     //在构造器中,女招待照顾两个菜单
    16     public Waitress(PancakeHouseMenu pancakeHouseMenu,DinerMenu dinerMenu){
    17         this.pancakeHouseMenu = pancakeHouseMenu;
    18         this.dinerMenu = dinerMenu;
    19     }
    20     
    21     public void printMenu(){
    22         Iterator pancakeIterator = pancakeHouseMenu.createIterator();
    23         Iterator dinerIterator = dinerMenu.createIterator();
    24         
    25         System.out.println("MENU:
    ----
    BREAKFAST");
    26         //打印煎饼屋菜单
    27         printMenu(pancakeIterator);
    28         System.out.println("
    LUNCH");
    29         //打印餐厅菜单
    30         printMenu(dinerIterator);
    31     }
    32 
    33 
    34     /**
    35     * 打印菜单
    36     * @Description:  
    37     * @param iterator 
    38     * @author xingle
    39     * @date 2014年8月1日 下午3:29:11
    40     */
    41     private void printMenu(Iterator iterator) {
    42         while(iterator.hasNext()){
    43             MenuItem menuItem = (MenuItem) iterator.next();
    44             System.out.println("名称:"+menuItem.getName()+",");
    45             System.out.println("价格:"+menuItem.getPrice()+",");
    46             System.out.println("描述:"+menuItem.getDescription());
    47             System.out.println("");
    48         }
    49     }
    50     
    51     
    52 
    53 }

    最后,我们写一个测试程序,看看女招待如何工作:

     1 package firsthead.interater;
     2 
     3 /**
     4  * @ClassName: MenuTestDrive
     5  * @Description: 测试程序
     6  * @author xingle
     7  * @date 2014年8月1日 下午3:35:05
     8  */
     9 public class MenuTestDrive {
    10     public static void main(String[] args){
    11         PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
    12         DinerMenu dinerMenu = new DinerMenu();
    13         
    14         Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);
    15         waitress.printMenu();
    16     }
    17 }

    执行以上程序,结果如下:

    MENU:

    ----

    BREAKFAST

    名称:K&B's Pancake Breakfast,

    价格:2.99,

    描述:Pancakes with scrambled eggs, and toast

    名称:Regular Pancake Breakfast,

    价格:2.99,

    描述:Pancakes with fried eggs, sausage

    名称:Blueberry Pancakes,

    价格:3.49,

    描述:Pancakes made with fresh blueberries

    名称:Waffles,

    价格:3.59,

    描述:Waffles, with your choice of blueberries or strawberries

    LUNCH

    名称:Vegetarian BLT,

    价格: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,

    描述: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,

    价格:3.99,

    描述:Steamed vegetables over brown rice

    名称:Pasta,

    价格:3.89,

    描述:Spaghetti with Marinara Sauce, and a slice of sourdough bread

    到目前为止,我们做了哪些工作呢:

    1.菜单的实现已经封装起来了。女招待不知道菜单式如何存储菜单项集合的。

    2.只要实现迭代,我们只需要一个循环,就可以多态地处理任何项的集合。

    3.女招待现在只使用一个借口(迭代器)

    现在菜单的接口完全一样,但是,还没有一个共同的接口,也就是说女招待仍然捆绑于两个具体的菜单类,下面我们再来修改一下。

     先来看看目前的设计:

    你可能会奇怪,为什么我们不用java的Iterator接口呢——之所以这么做,是为了让你了解如何从头创建一个迭代器。现在目的已经达到了,所以就要改变做法,开始使用java的Iterator接口了。

    我们只需要将煎饼屋迭代器和餐厅迭代器所扩展的接口,即由我们自己的迭代器接口,改为java.util的迭代器接口即可。实际上,甚至更简单。其实不止java.util有迭代器接口,ArrayList也有一个返回一个迭代器的iterator()方法。换句话说,我们并不需要为ArrayList实现自己的迭代器。然后,这里我们仍然需要为餐厅菜单实现一个迭代器,因为餐厅菜单使用的是数组,而数组不支持iterator()方法。

    我们从煎饼屋菜单开始,只需要删除煎饼屋迭代器类,然后在煎饼屋的代码前加上import java.util.Iterator,再改变下面这一行代码就可以了:

    这样PancakeHouseMenu就完成了。

    接着,处理DinerMenu,以符合java.util.Iterator的需求。

    然后,我们需要给菜单一个共同的接口,然后再改一下女招待。

    现在,我们需要让煎饼屋菜单类和餐厅菜单类都实现Menu接口,然后更新女招待的代码如下:

    煎饼屋菜单和餐厅菜单的类,都实现了Menu接口,女招待可以利用接口(而不是具体的类)引用每一个菜单对象。这样,通过“针对接口编程,而不是针对实现编程”,我们就可以减少女招待和具体类之间的依赖。

    下面看下具体的代码:

    首先是煎饼屋菜单:

     1 package firsthead.interater;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /**
     7  * @ClassName: PancakeHouseMenu
     8  * @Description: 煎饼屋菜单
     9  * @author xingle
    10  * @date 2014年7月31日 下午10:09:33
    11  */
    12 public class PancakeHouseMenu implements Menu{
    13 
    14     ArrayList menuItems;
    15 
    16     public PancakeHouseMenu() {
    17         // 使用ArrayList存储菜单项
    18         menuItems = new ArrayList();
    19 
    20         addItem("K&B's Pancake Breakfast",
    21                 "Pancakes with scrambled eggs, and toast", true, 2.99);
    22 
    23         addItem("Regular Pancake Breakfast",
    24                 "Pancakes with fried eggs, sausage", false, 2.99);
    25 
    26         addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries",
    27                 true, 3.49);
    28 
    29         addItem("Waffles",
    30                 "Waffles, with your choice of blueberries or strawberries",
    31                 true, 3.59);
    32 
    33     }
    34 
    35     // 加入一个菜单项:创建一个新的菜单项对象,加入ArrayList
    36     public void addItem(String name, String description, boolean vegetarian,
    37             double price) {
    38         MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
    39         menuItems.add(menuItem);
    40     }
    41     
    42     //返回菜单项列表
    43     public ArrayList getMenuItems(){
    44         return menuItems;
    45     }
    46     
    47     public Iterator createIterator(){
    48         //return new PancakeHouseMenuIterator(menuItems);
    49         //这里不创建自己的迭代器,而是调用菜单项ArrayList的iterator()方法
    50         return menuItems.iterator();
    51     } 
    52 
    53 }

    餐厅菜单:

     1 package firsthead.interater;
     2 
     3 import java.util.Iterator;
     4 
     5 /**
     6  * @ClassName: DinerMenu
     7  * @Description: 餐厅菜单
     8  * @author xingle
     9  * @date 2014年7月31日 下午10:18:17
    10  */
    11 public class DinerMenu implements Menu{
    12 
    13     static final int MAX_ITEMS = 6;
    14     int numberOfItems = 0;
    15     // 使用数组形式
    16     MenuItem[] menuItems;
    17 
    18     public DinerMenu() {
    19         menuItems = new MenuItem[MAX_ITEMS];
    20 
    21         addItem("Vegetarian BLT",
    22                 "(Fakin') Bacon with lettuce & tomato on whole wheat", true,
    23                 2.99);
    24         addItem("BLT", "Bacon with lettuce & tomato on whole wheat", false,
    25                 2.99);
    26         addItem("Soup of the day",
    27                 "Soup of the day, with a side of potato salad", false, 3.29);
    28         addItem("Hotdog",
    29                 "A hot dog, with saurkraut, relish, onions, topped with cheese",
    30                 false, 3.05);
    31         addItem("Steamed Veggies and Brown Rice",
    32                 "Steamed vegetables over brown rice", true, 3.99);
    33         addItem("Pasta",
    34                 "Spaghetti with Marinara Sauce, and a slice of sourdough bread",
    35                 true, 3.89);
    36     }
    37 
    38     public void addItem(String name, String description, boolean vegetarian,
    39             double price) {
    40         MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
    41         if (numberOfItems >= MAX_ITEMS) {
    42             System.err.println("Sorry, menu is full!  Can't add item to menu");
    43         } else {
    44             menuItems[numberOfItems] = menuItem;
    45             numberOfItems = numberOfItems + 1;
    46         }
    47     }
    48 
    49     public MenuItem[] getMenuItems() {
    50         return menuItems;
    51     }
    52     
    53     public Iterator createIterator(){
    54         return new DinerMenuIterator(menuItems);
    55     }
    56 
    57     // 其他的方法
    58 }

    餐厅迭代器:

     1 package firsthead.interater;
     2 
     3 import java.util.Iterator;//将引用的迭代器换成这个
     4 
     5 /**
     6  * @ClassName: DinerMenuIterator
     7  * @Description: 实现一个具体的迭代器,餐厅菜单迭代器
     8  * @author xingle
     9  * @date 2014年8月1日 下午1:56:06
    10  */
    11 public class DinerMenuIterator implements Iterator{
    12     
    13     MenuItem[] items;
    14     //记录当前数组遍历的位置
    15     int position = 0;
    16     
    17     //构造器需要传入一个菜单项的数组当做参数
    18     public DinerMenuIterator(MenuItem[] items){
    19         this.items = items;
    20     }
    21 
    22     /* 
    23     * Title: hasNext
    24     * Description: 
    25     * @return 
    26     * @see firsthead.interater.Iterator#hasNext() 
    27     */
    28     @Override
    29     public boolean hasNext() {
    30         if (position >= items.length || items[position] == null) {
    31             return false;
    32         } else {
    33             return true;
    34         }
    35     }
    36 
    37     /* 
    38     * Title: next
    39     * Description: 返回数组下一项并递增其位置
    40     * @return 
    41     * @see firsthead.interater.Iterator#next() 
    42     */
    43     @Override
    44     public Object next() {
    45         MenuItem menuItem = items[position];
    46         position = position + 1;
    47         return menuItem;
    48     }
    49 
    50     
    51     /*上面都没有动*/
    52     
    53     /* 
    54     * Title: remove
    55     * Description:  需要自己实现remove()方法,因为使用的是固定长度的数组,在remove()调用是后面的元素需要向前移动
    56     * @see java.util.Iterator#remove() 
    57     */
    58     @Override
    59     public void remove() {
    60         if (position <= 0) {
    61             throw new IllegalStateException
    62                 ("You can't remove an item until you've done at least one next()");
    63         }
    64         if (items[position-1] != null) {
    65             for (int i = position-1; i < (items.length-1); i++) {
    66                 items[i] = items[i+1];
    67             }
    68             items[items.length-1] = null;
    69         }
    70     }
    71 
    72 }

    女招待:

     1 package firsthead.interater;
     2 
     3 import java.util.Iterator;
     4 
     5 /**
     6  * @ClassName: Waitress
     7  * @Description: 女招待的代码
     8  * @author xingle
     9  * @date 2014年8月1日 下午2:19:46
    10  */
    11 public class Waitress {
    12     //煎饼屋菜单
    13     Menu pancakeHouseMenu;
    14     //餐厅菜单
    15     Menu dinerMenu;
    16     
    17     /*//在构造器中,女招待照顾两个菜单
    18     public Waitress(PancakeHouseMenu pancakeHouseMenu,DinerMenu dinerMenu){
    19         this.pancakeHouseMenu = pancakeHouseMenu;
    20         this.dinerMenu = dinerMenu;
    21     }*/
    22     
    23     public Waitress(Menu pancakeHouseMenu,Menu dinerMenu){
    24         this.pancakeHouseMenu = pancakeHouseMenu;
    25         this.dinerMenu = dinerMenu;
    26     }
    27     
    28     public void printMenu(){
    29         Iterator pancakeIterator = pancakeHouseMenu.createIterator();
    30         Iterator dinerIterator = dinerMenu.createIterator();
    31         
    32         System.out.println("MENU:
    ----
    BREAKFAST");
    33         //打印煎饼屋菜单
    34         printMenu(pancakeIterator);
    35         System.out.println("
    LUNCH");
    36         //打印餐厅菜单
    37         printMenu(dinerIterator);
    38     }
    39 
    40 
    41     /**
    42     * 打印菜单
    43     * @Description:  
    44     * @param iterator 
    45     * @author xingle
    46     * @date 2014年8月1日 下午3:29:11
    47     */
    48     private void printMenu(Iterator iterator) {
    49         while(iterator.hasNext()){
    50             MenuItem menuItem = (MenuItem) iterator.next();
    51             System.out.println("名称:"+menuItem.getName()+",");
    52             System.out.println("价格:"+menuItem.getPrice()+",");
    53             System.out.println("描述:"+menuItem.getDescription());
    54             System.out.println("");
    55         }
    56     }
    57     
    58     
    59 
    60 }

    其中Menu:

     1 package firsthead.interater;
     2 
     3 import java.util.Iterator;
     4 
     5 /**
     6  * @ClassName: Menu
     7  * @Description: TODO
     8  * @author xingle
     9  * @date 2014年8月1日 下午4:36:46
    10  */
    11 public interface Menu {
    12     public Iterator createIterator();
    13 }

    测试程序不变:

     1 package firsthead.interater;
     2 
     3 /**
     4  * @ClassName: MenuTestDrive
     5  * @Description: 测试程序
     6  * @author xingle
     7  * @date 2014年8月1日 下午3:35:05
     8  */
     9 public class MenuTestDrive {
    10     public static void main(String[] args){
    11         PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
    12         DinerMenu dinerMenu = new DinerMenu();
    13         
    14         Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);
    15         waitress.printMenu();
    16     }
    17 
    18 }

    执行结果同上。

    第2部分 迭代器模式定义

     迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

    迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露其内部的表示。

    把游走的任务放在迭代器上,而不是聚合上。这样简化了聚合的接口和实现,也让责任各得其所。

    迭代器模式的类图:

     

    单一责任

    如果允许我们的聚合实现它们内部的集合以及相关的操作和遍历的方法,又会如何?

    这样做不好,因为这样我们给了这个类两个变化的原因:如果这个集合改变,这个类必须跟着改变;如果遍历的方式改变,这个类也必须跟着改变。

    设计原则:一个类应该只有一个引起变化的原因。

    这个原则告诉我们,应该尽量让每个类保持单一责任。

    问题2 引入 对象村的咖啡厅也要并入进来,供应晚餐菜单

    咖啡厅菜单如下:

     

    下面重新做咖啡厅的代码:

     1 package firsthead.interater;
     2 
     3 import java.util.Hashtable;
     4 import java.util.Iterator;
     5 
     6 /**
     7  * 
     8  * @ClassName: CafeMenu 咖啡馆菜单
     9  * @author Xingle
    10  * @date 2014-8-4 下午12:55:13
    11  */
    12 public class CafeMenu implements Menu{
    13     // 菜单项用hashtable形式
    14     Hashtable menuItems = new Hashtable();
    15 
    16     public CafeMenu() {
    17         addItem("Veggie Burger and Air Fries",
    18                 "Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
    19                 true, 3.99);
    20         addItem("Soup of the day",
    21                 "A cup of the soup of the day, with a side salad", false, 3.69);
    22         addItem("Burrito",
    23                 "A large burrito, with whole pinto beans, salsa, guacamole",
    24                 true, 4.29);
    25     }
    26 
    27     public void addItem(String name, String description, boolean vegetarian,
    28             double price) {
    29         MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
    30         menuItems.put(menuItem.getName(), menuItem);
    31 
    32     }
    33 
    34     public Hashtable getItems() {
    35         return menuItems;
    36     }
    37 
    38     /**
    39      * 
    40      * @Description: TODO
    41      * @return
    42      * @author xingle
    43      * @data 2014-8-4 下午1:50:59
    44      */
    45     @Override
    46     public Iterator createIterator() {
    47         return menuItems.values().iterator();
    48     }
    49 
    50 }

    改写女招待,让她认识咖啡厅菜单:

     1 package firsthead.interater;
     2 
     3 import java.util.Iterator;
     4 
     5 /**
     6  * @ClassName: Waitress
     7  * @Description: 女招待的代码
     8  * @author xingle
     9  * @date 2014年8月1日 下午2:19:46
    10  */
    11 public class Waitress {
    12     //煎饼屋菜单
    13     Menu pancakeHouseMenu;
    14     //餐厅菜单
    15     Menu dinerMenu;
    16     //增加咖啡厅菜单
    17     Menu cafeMenu;
    18     
    19     /*//在构造器中,女招待照顾两个菜单
    20     public Waitress(PancakeHouseMenu pancakeHouseMenu,DinerMenu dinerMenu){
    21         this.pancakeHouseMenu = pancakeHouseMenu;
    22         this.dinerMenu = dinerMenu;
    23     }*/
    24     
    25     /*public Waitress(Menu pancakeHouseMenu,Menu dinerMenu){
    26         this.pancakeHouseMenu = pancakeHouseMenu;
    27         this.dinerMenu = dinerMenu;
    28     }*/
    29     
    30     public Waitress(Menu pancakeHouseMenu,Menu dinerMenu,Menu cafeMenu){
    31         this.pancakeHouseMenu = pancakeHouseMenu;
    32         this.dinerMenu = dinerMenu;
    33         this.cafeMenu = cafeMenu;
    34     }
    35     
    36     public void printMenu(){
    37         Iterator pancakeIterator = pancakeHouseMenu.createIterator();
    38         Iterator dinerIterator = dinerMenu.createIterator();
    39         Iterator cafeIterator = cafeMenu.createIterator();//
    40         
    41         System.out.println("MENU:
    ----
    BREAKFAST");
    42         //打印煎饼屋菜单
    43         printMenu(pancakeIterator);
    44         System.out.println("
    LUNCH");
    45         //打印餐厅菜单
    46         printMenu(dinerIterator);
    47         //咖啡厅菜单,打印出来
    48         System.out.println("
    DINNER");
    49         printMenu(cafeIterator);
    50     }
    51 
    52 
    53     /**
    54     * 打印菜单
    55     * @Description:  
    56     * @param iterator 
    57     * @author xingle
    58     * @date 2014年8月1日 下午3:29:11
    59     */
    60     private void printMenu(Iterator iterator) {
    61         while(iterator.hasNext()){
    62             MenuItem menuItem = (MenuItem) iterator.next();
    63             System.out.println("名称:"+menuItem.getName()+",");
    64             System.out.println("价格:"+menuItem.getPrice()+",");
    65             System.out.println("描述:"+menuItem.getDescription());
    66             System.out.println("");
    67         }
    68     }
    69     
    70     
    71 
    72 }

    最后测试程序:

     1 package firsthead.interater;
     2 
     3 /**
     4  * @ClassName: MenuTestDrive
     5  * @Description: 测试程序
     6  * @author xingle
     7  * @date 2014年8月1日 下午3:35:05
     8  */
     9 public class MenuTestDrive {
    10     public static void main(String[] args){
    11         PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
    12         DinerMenu dinerMenu = new DinerMenu();
    13         CafeMenu cafeMenu = new CafeMenu();
    14         
    15         //Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);
    16         Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu);
    17         waitress.printMenu();
    18     }
    19 
    20 }

    执行结果:

    MENU:
    ----
    BREAKFAST
    名称:K&B's Pancake Breakfast,
    价格:2.99,
    描述:Pancakes with scrambled eggs, and toast

    名称:Regular Pancake Breakfast,
    价格:2.99,
    描述:Pancakes with fried eggs, sausage

    名称:Blueberry Pancakes,
    价格:3.49,
    描述:Pancakes made with fresh blueberries

    名称:Waffles,
    价格:3.59,
    描述:Waffles, with your choice of blueberries or strawberries


    LUNCH
    名称:Vegetarian BLT,
    价格: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,
    描述: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,
    价格:3.99,
    描述:Steamed vegetables over brown rice

    名称:Pasta,
    价格:3.89,
    描述:Spaghetti with Marinara Sauce, and a slice of sourdough bread


    DINNER
    名称:Soup of the day,
    价格:3.69,
    描述:A cup of the soup of the day, with a side salad

    名称:Burrito,
    价格:4.29,
    描述:A large burrito, with whole pinto beans, salsa, guacamole

    名称:Veggie Burger and Air Fries,
    价格:3.99,
    描述:Veggie burger on a whole wheat bun, lettuce, tomato, and fries

    问题3 引入 

    这时,餐厅需要创建一份甜点菜单,并将它插入到常规菜单中。即要支持菜单中的菜单。

     

     下面将引入下一模式——组合模式

  • 相关阅读:
    MySQL存储过程参数【4】
    MySQL存储过程的变量【3】
    MySQL存储过程入门【2】
    MySQL存储过程简介【1】
    MySQL删除重复行的方式
    在MySQL单个表中找到重复的值
    MySQL比较两个表不同的数据
    【思维】P5743 【深基7.习8】猴子吃桃——有趣的解法,归纳推导
    对判断质数的算法的优化
    【思维】P1321 单词覆盖还原
  • 原文地址:https://www.cnblogs.com/xingele0917/p/3878831.html
Copyright © 2011-2022 走看看