設計模式目錄:html
設計模式 1 ——觀察者模式java
設計模式 6 —— 單件模式post
設計模式 9 —— 模板方法模式this
概要
餐廳和煎餅屋要合併,要把煎餅屋的菜單做爲早餐菜單,餐廳的菜單做爲午飯菜單。可是對於菜單項的記錄,前者用的是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 + "\n " + 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:\n----\nBREAKFAST"); 26 //打印煎餅屋菜單 27 printMenu(pancakeIterator); 28 System.out.println("\nLUNCH"); 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:\n----\nBREAKFAST"); 33 //打印煎餅屋菜單 34 printMenu(pancakeIterator); 35 System.out.println("\nLUNCH"); 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 引入 對象村的咖啡廳也要併入進來,供應晚餐菜單
咖啡廳菜單以下:
下面從新作咖啡廳的代碼:
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:\n----\nBREAKFAST"); 42 //打印煎餅屋菜單 43 printMenu(pancakeIterator); 44 System.out.println("\nLUNCH"); 45 //打印餐廳菜單 46 printMenu(dinerIterator); 47 //咖啡廳菜單,打印出來 48 System.out.println("\nDINNER"); 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 引入
這時,餐廳須要建立一份甜點菜單,並將它插入到常規菜單中。即要支持菜單中的菜單。
下面將引入下一模式——組合模式