设计模式学习--迭代器模式(Iterator Pattern)

概述

———————————————————————————————————————————————————

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

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

组合模式允许你将对象组成树形结构来表现“整体/部分”的层次结构。组合能让客户以一致的方式处理个别对象和对象组合。

OO原则

———————————————————————————————————————————————————

  • 封装变化
  • 多用组合,少用继承
  • 针对接口编程,不针对实现编程
  • 为交互对象之间的松耦合设计而努力
  • 类应该对扩展开放,对修改关闭
  • 依赖抽象,不要依赖具体类
  • 只和朋友交谈
  • 别找我,我会找你
  • 类应该只有一个改变的理由

要点

———————————————————————————————————————————————————

  • 迭代器允许访问聚合的元素,而不需要暴露它的内部结构
  • 迭代器将遍历聚合的工作封装进一个对象中。
  • 当使用迭代器的时候,我们依赖聚合提供遍历。
  • 迭代器提供一个通用的接口,让我们遍历聚合的项,当我们编码使用聚合的项时,就可以使用多态机制。
  • 我们应该努力让一个类分配一个责任。
  • 组合模式提供一个结构,可同时包容个别对象和组合对象。
  • 组合模式允许客户对个别对象以及组合对象一视同仁。
  • 组合结构内的任意对象成为组件,组件可以是组合,也可以是叶节点。
  • 在实现组合模式时,有许多设计上的折衷。你要根据需要平衡透明性和安全性。

例子1

———————————————————————————————————————————————————

自定义迭代器

Iterator

package dinermerger;/*** 自定义迭代器接口* @author wwj**/
public interface Iterator {boolean hasNext();       //是否还有下一个元素Object next();           //返回下一个元素
}

实现迭代器

DinerMenuIterator

package dinermerger;/*** 实现一个具体的迭代器,为餐厅菜单服务* @author Administrator**/
public class DinerMenuIterator implements Iterator {MenuItem[] items;int position = 0; //记录当前数组遍历的位置public DinerMenuIterator(MenuItem[] items) {this.items = items;}@Overridepublic boolean hasNext() {if(position >= items.length || items[position] == null) {return false;} else {return true;}}@Overridepublic Object next() {MenuItem menuItem = items[position];position = position + 1;return menuItem;}}

PancakeHouseMenuIterator

package dinermerger;import java.util.ArrayList;/*** 煎饼屋菜单实现* @author wwj**/
public class PancakeHouseMenuIterator implements Iterator{ArrayList menuItems;int position = 0;public PancakeHouseMenuIterator(ArrayList menuItems) {this.menuItems = menuItems;}@Overridepublic boolean hasNext() {if(position >= menuItems.size()) {return false;} else {return true;}}@Overridepublic Object next() {Object object = menuItems.get(position);position = position + 1;return object;}
}

菜单项

package dinermerger;/*** 菜单项* @author wwj**/
public class MenuItem {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;}}

菜单

PancakeHouseMenu

package dinermerger;import java.util.ArrayList;/*** 煎饼屋菜单实现* @author wwj**/
public class PancakeHouseMenu {ArrayList menuItems;public PancakeHouseMenu() {menuItems = new ArrayList();addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99);addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99);addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49);addItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59);}/*** 创建一个新的菜单项对象,传入每一个变量,然后将它加入到ArrayList中* @param name   菜名* @param description 叙述* @param vegetarian  是否为素食* @param price    价格*/public void addItem(String name, String description, boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);menuItems.add(menuItem);}/* public ArrayList getMenuItems() {return menuItems;}*/public Iterator createIterator() {return new PancakeHouseMenuIterator(menuItems);}
}

DinerMenu

package dinermerger;/*** 餐厅菜单* @author wwj**/
public class DinerMenu {static final int MAX_ITEMS = 6;int numberOfItems = 0;MenuItem[] menuItems;        //使用一个数组,所以可以控制菜单的长度,并且在取出菜单项的时候,不需要转型public DinerMenu() {menuItems = new MenuItem[MAX_ITEMS];addItem("Vegetarian BLT","(Fakin') Bacon with lettuce & tomato on Whole wheat", true,2.99);addItem("BLT", "Bacon with lettuce & tomato on Whole wheat", false,2.99);addItem("Soup of the day","Soup of the day, with a side of potato salad", false, 3.29);addItem("Hotdog","A hot dog, with saurkraut, relish, onions, topped with cheese",false, 3.05);addItem("Steamed Veggies and Brown Rice","Steamed vegetables over brown rice", true, 3.99);addItem("Pasta","Spaghetti with Marinara Sauce, and a slice of sourdough bread",true, 3.89);}private void addItem(String name, String description, boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);if(numberOfItems >= MAX_ITEMS) {System.err.println("Sorry, menu is full! Can't add item to menu");} else {menuItems[numberOfItems] = menuItem;numberOfItems = numberOfItems + 1;}}/*** 增加了迭代器之后,这个方法就不需要了* @return*//*public MenuItem[] getMenuItems() {return menuItems;}*//*** 用来从菜单项数组创建一个DinerMenuIterator,并将它返回给客户*/public Iterator createIterator() {return new DinerMenuIterator(menuItems);}}

女招待

package dinermerger;/*** 女招待类* @author wwj**/
public class Waitress {PancakeHouseMenu pancakeHouseMenu;DinerMenu dinerMenu;/***在这个构造器中,女招待照顾两个菜单* @param pancakeHouseMenu* @param dinerMenu*/public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;}public void printMenu() {Iterator pancakeIterator = pancakeHouseMenu.createIterator();Iterator dinerIterator = dinerMenu.createIterator();System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);}private void printMenu(Iterator iterator) {while(iterator.hasNext()) {   //测试是否还有其他项MenuItem menuItem = (MenuItem) iterator.next();System.out.print(menuItem.getName() + ", ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}}
}

测试

package dinermerger;public class MenuTestDrive {/*** @param args*/public static void main(String[] args) {PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();DinerMenu dinerMenu = new DinerMenu();Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);waitress.printMenu();}}

结果:

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

例子2

———————————————————————————————————————————————————

利用java.util.Iterator来实现迭代器

提供一个Menu接口,让PancakeHouseMenu和DinerMenu都实现Menu接口,这样Waitress就不用依赖具体的菜单的问题,也不用依赖菜单项的具体实现了。

package dinermerger;/*** 这是一个简单的接口,让客户能够取得菜单项* @author Administrator**/
public interface Menu {public Iterator createIterator();
}

DinerMenu

package dinermerger;import java.util.Iterator;/*** 餐厅菜单* @author wwj**/
public class DinerMenu implements Menu{static final int MAX_ITEMS = 6;int numberOfItems = 0;MenuItem[] menuItems;     //使用一个数组,所以可以控制菜单的长度,并且在取出菜单项的时候,不需要转型public DinerMenu() {menuItems = new MenuItem[MAX_ITEMS];addItem("Vegetarian BLT","(Fakin') Bacon with lettuce & tomato on Whole wheat", true,2.99);addItem("BLT", "Bacon with lettuce & tomato on Whole wheat", false,2.99);addItem("Soup of the day","Soup of the day, with a side of potato salad", false, 3.29);addItem("Hotdog","A hot dog, with saurkraut, relish, onions, topped with cheese",false, 3.05);addItem("Steamed Veggies and Brown Rice","Steamed vegetables over brown rice", true, 3.99);addItem("Pasta","Spaghetti with Marinara Sauce, and a slice of sourdough bread",true, 3.89);}private void addItem(String name, String description, boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);if(numberOfItems >= MAX_ITEMS) {System.err.println("Sorry, menu is full! Can't add item to menu");} else {menuItems[numberOfItems] = menuItem;numberOfItems = numberOfItems + 1;}}/*** 增加了迭代器之后,这个方法就不需要了* @return*//*public MenuItem[] getMenuItems() {return menuItems;}*//*** 用来从菜单项数组创建一个DinerMenuIterator,并将它返回给客户*/public Iterator createIterator() {return new DinerMenuIterator(menuItems);}}

PancakeHouseMenu

不创建自己的迭代器,而是调用ArrayList的iterator()方法

package dinermerger;import java.util.ArrayList;
import java.util.Iterator;/*** 煎饼屋菜单实现* @author wwj**/
public class PancakeHouseMenu implements Menu{ArrayList menuItems;public PancakeHouseMenu() {menuItems = new ArrayList();addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99);addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99);addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49);addItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59);}/*** 创建一个新的菜单项对象,传入每一个变量,然后将它加入到ArrayList中* @param name    菜名* @param description 叙述* @param vegetarian  是否为素食* @param price    价格*/public void addItem(String name, String description, boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);menuItems.add(menuItem);}/* public ArrayList getMenuItems() {return menuItems;}*/public Iterator createIterator() {return menuItems.iterator();}
}

DinerMenuIterator

package dinermerger;import java.util.Iterator;/*** 实现一个具体的迭代器,为餐厅菜单服务* @author Administrator**/
public class DinerMenuIterator implements Iterator {MenuItem[] items;int position = 0; //记录当前数组遍历的位置public DinerMenuIterator(MenuItem[] items) {this.items = items;}@Overridepublic boolean hasNext() {if(position >= items.length || items[position] == null) {return false;} else {return true;}}@Overridepublic Object next() {MenuItem menuItem = items[position];position = position + 1;return menuItem;}@Overridepublic void remove() {if(position <= 0) {throw new IllegalStateException("You can't remove an item until you've done at least on next()");}if(items[position - 1] != null) {for(int i = position - 1; i < (items.length - 1); i++) {items[i] = items[i + 1];}items[items.length - 1] = null;}}}

Waitress

package dinermerger;import java.util.Iterator; /*** 女招待类* @author wwj**/
public class Waitress {
//  PancakeHouseMenu pancakeHouseMenu;
//  DinerMenu dinerMenu;Menu pancakeHouseMenu;Menu dinerMenu;/***在这个构造器中,女招待照顾两个菜单* @param pancakeHouseMenu* @param dinerMenu*/public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;}public void printMenu() {Iterator pancakeIterator = pancakeHouseMenu.createIterator();Iterator dinerIterator = dinerMenu.createIterator();System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);}private void printMenu(Iterator iterator) {while(iterator.hasNext()) {    //测试是否还有其他项MenuItem menuItem = (MenuItem) iterator.next();System.out.print(menuItem.getName() + ", ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}}
}

增加一个CafeMenu会变成以下这样

package dinermerger;import java.util.Hashtable;
import java.util.Iterator;/*** 咖啡厅菜单* @author wwj**/
public class CafeMenu implements Menu{Hashtable menuItems = new Hashtable();public CafeMenu() {addItem("Veggie Burger and Air Fries","Veggie burger on a whole wheat bun, lettuce, tomato, and fries",true, 3.99);addItem("Soup of the day","A cup of the soup of the day, with a side salad", false, 3.69);addItem("Burrito","A large burrito, with whole pinto beans, salsa, guacamole",true, 4.29);}public void addItem(String name, String description, boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);menuItems.put(menuItem.getName(), menuItem);}@Overridepublic Iterator createIterator() {return menuItems.values().iterator();}
}
package dinermerger;import java.util.Iterator; /*** 女招待类* @author wwj**/
public class Waitress {
//  PancakeHouseMenu pancakeHouseMenu;
//  DinerMenu dinerMenu;Menu pancakeHouseMenu;Menu dinerMenu;Menu cafeMenu; /***在这个构造器中,女招待照顾两个菜单* @param pancakeHouseMenu* @param dinerMenu*/public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;}/*** 新增一个cafeMenu* @param pancakeHouseMenu* @param dinerMenu* @param cafeMenu*/public Waitress(Menu pancakeHouseMenu, Menu dinerMenu, Menu cafeMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;this.cafeMenu = cafeMenu;}public void printMenu() {Iterator pancakeIterator = pancakeHouseMenu.createIterator();Iterator dinerIterator = dinerMenu.createIterator();Iterator cafeIterator = cafeMenu.createIterator();System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);System.out.println("\nDINNER");printMenu(cafeIterator);}private void printMenu(Iterator iterator) {while(iterator.hasNext()) {   //测试是否还有其他项MenuItem menuItem = (MenuItem) iterator.next();System.out.print(menuItem.getName() + ", ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}}
}

Test

package dinermerger;public class MenuTestDrive {/*** @param args*/public static void main(String[] args) {
//      PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
//      DinerMenu dinerMenu = new DinerMenu();
//
//      Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);
//
//      waitress.printMenu();Menu pancakeHouseMenu = new PancakeHouseMenu();Menu dinerMenu = new DinerMenu();Menu cafeMenu = new CafeMenu();Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu);waitress.printMenu();}}

结果:

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

为了让女招待更加有扩展性,我们这样做

package dinermerger;import java.util.ArrayList;
import java.util.Iterator; /*** 女招待类* @author wwj**/
public class Waitress {
//  PancakeHouseMenu pancakeHouseMenu;
//  DinerMenu dinerMenu;
//  Menu pancakeHouseMenu;
//  Menu dinerMenu;
//  Menu cafeMenu;  ArrayList menus;/*  *//***在这个构造器中,女招待照顾两个菜单* @param pancakeHouseMenu* @param dinerMenu*//*public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;}*//*** 新增一个cafeMenu* @param pancakeHouseMenu* @param dinerMenu* @param cafeMenu*//*public Waitress(Menu pancakeHouseMenu, Menu dinerMenu, Menu cafeMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;this.cafeMenu = cafeMenu;}*/public Waitress(ArrayList menus) {this.menus = menus;}public void printMenu() {
/*      Iterator pancakeIterator = pancakeHouseMenu.createIterator();Iterator dinerIterator = dinerMenu.createIterator();Iterator cafeIterator = cafeMenu.createIterator();System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);System.out.println("\nDINNER");printMenu(cafeIterator);*/Iterator menuIterator = menus.iterator();while(menuIterator.hasNext()) {Menu menu = (Menu)menuIterator.next();printMenu(menu.createIterator());}}private void printMenu(Iterator iterator) {while(iterator.hasNext()) {    //测试是否还有其他项MenuItem menuItem = (MenuItem) iterator.next();System.out.print(menuItem.getName() + ", ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}}
}

测试的时候我们就可以这样

package dinermerger;import java.util.ArrayList;public class MenuTestDrive {/*** @param args*/public static void main(String[] args) {
//      PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
//      DinerMenu dinerMenu = new DinerMenu();
//
//      Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);
//
//      waitress.printMenu();Menu pancakeHouseMenu = new PancakeHouseMenu();Menu dinerMenu = new DinerMenu();Menu cafeMenu = new CafeMenu();//       Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu);ArrayList menus = new ArrayList();menus.add(pancakeHouseMenu);menus.add(dinerMenu);menus.add(cafeMenu);Waitress waitress = new Waitress(menus);waitress.printMenu();}}

总算是要讲到组合模式了,这个模式可以解决迭代器模式中不能解决的一个难题。比如我们需要一个新的餐单,菜单下又有子菜单,单单用迭代器就不能解决了。这时候可以考虑到用树形结构来展示整个菜单。

看下面的例子吧:

定义菜单组件

package composite;/*** 定义菜单组件* @author Administrator**/
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 void print() {throw new UnsupportedOperationException();}
}

实现菜单

package composite;import java.util.ArrayList;
import java.util.Iterator;public class Menu extends MenuComponent{ArrayList menuComponents = new ArrayList();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 (MenuComponent) menuComponents.get(i);}public String getName() {return name;}public String getDescription() {return description;}public void print(){System.out.print("\n" + getName());System.out.println(", " + getDescription());System.out.println("------------------");Iterator iterator = menuComponents.iterator();while(iterator.hasNext()) {MenuComponent menuComponent =(MenuComponent) iterator.next();menuComponent.print();}}
}


实现菜单项

package composite;/*** 实现菜单项* @author Administrator**/
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 boolean isVegetarian() {return vegetarian;}public double getPrice() {return price;}public void print(){System.out.print("  " + getName());if(isVegetarian()) {System.out.print("(v)");}System.out.println("," + getPrice());System.out.println("    --" + getDescription());}}

变得十分快乐的女招待

package composite;import java.util.Iterator;public class Waitress {MenuComponent allMenus;public Waitress(MenuComponent allMenus) {this.allMenus = allMenus;}public void printMenu() {allMenus.print();}
}

我们这样测试

package composite;import java.util.*;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 coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee");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","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));cafeMenu.add(coffeeMenu);coffeeMenu.add(new MenuItem("Coffee Cake","Crumbly cake topped with cinnamon and walnuts",true,1.59));coffeeMenu.add(new MenuItem("Bagel","Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",false,0.69));coffeeMenu.add(new MenuItem("Biscotti","Three almond or hazelnut biscotti cookies",true,0.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
    --Steamed vegetables over brown rice
  Pasta(v),3.89
    --Spaghetti with Marinara Sauce, and a slice of sourdough bread

DESSERT MENU, Dessert of course!
------------------
  Apple Pie(v),1.59
    --Apple pie with a flakey crust, topped with vanilla icecream
  Cheesecake(v),1.99
    --Creamy New York cheesecake, with a chocolate graham crust
  Sorbet(v),1.89
    --A scoop of raspberry and a scoop of lime

CAFE MENU, Dinner
------------------
  Veggie Burger and Air Fries(v),3.99
    --Veggie burger on a whole wheat bun, lettuce, tomato, and fries
  Soup of the day,3.69
    --A cup of the soup of the day, with a side salad
  Burrito(v),4.29
    --A large burrito, with whole pinto beans, salsa, guacamole

COFFEE MENU, Stuff to go with your afternoon coffee
------------------
  Coffee Cake(v),1.59
    --Crumbly cake topped with cinnamon and walnuts
  Bagel,0.69
    --Flavors include sesame, poppyseed, cinnamon raisin, pumpkin
  Biscotti(v),0.89
    --Three almond or hazelnut biscotti cookies

转载于:https://www.cnblogs.com/xinyuyuanm/p/3177778.html

设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)相关推荐

  1. 设计模式总结之Composite Pattern(组合模式)

    Composite Pattern(组合模式) 组合模式,将对象组合成树形结构以表示"部分-整体"的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 有时候又叫做部 ...

  2. 设计模式GOF23之-------------------结构型模式(适配器模式、代理模式、桥接模式、装饰模式、组合模式、外观模式、享元模式)

    一 结构型模式 二 适配器模式 下面我将用代码模拟键盘usb接口和ps/2的转接器 的适配器过程: 首先定义客户需求: package GOF23;public interface Target {v ...

  3. 《Head First设计模式》第九章(2)组合模式

    组合模式 ​ 基于前一篇迭代模式的案例进行需求更新,餐厅的菜单管理系统需要有煎饼屋菜单和披萨菜单.现在希望在披萨菜单中能够加上一份餐后甜点的子菜单. 在迭代模式中,披萨菜单是用数组维护的,我们需要让披 ...

  4. No2.大话设计模式学习之策略、装饰、代理模式

    No2.大话设计模式学习之策略.装饰.代理模式 2.设计模式-part1 2.1.策略模式 描述:策略模式(Strategy Pattern)指同系列算法家族,内部都遵从算法家族的接口及方法规范 原则 ...

  5. 设计模式学习笔记(十七)——Command命令模式

    设计模式学习笔记(十七)--Command命令模式 Command命令模式介绍: Command命令模式是一种对象行为型模式,它主要解决的问题是:在软件构建过程中,"行为请求者"与 ...

  6. 设计模式学习笔记——责任链(Chain of Responsibility)模式

    设计模式学习笔记--责任链(Chain of Responsibility)模式 @(设计模式)[设计模式, 责任链模式, chain of responsibility] 设计模式学习笔记责任链Ch ...

  7. 设计模式学习(四):Strategy策略模式

    一.什么是Strategy模式 Strategy的意思是"策略",指的是与敌军对垒时行军作战的方法.在编程中,我们可以将它理解为"算法".无论什么程序,其目的都 ...

  8. java/android 设计模式学习笔记(3)---工厂方法模式

    这篇来介绍一下工厂方法模式(Factory Method Pattern),在实际开发过程中我们都习惯于直接使用 new 关键字用来创建一个对象,可是有时候对象的创造需要一系列的步骤:你可能需要计算或 ...

  9. java/android 设计模式学习笔记(7)---装饰者模式

    这篇将会介绍装饰者模式(Decorator Pattern),装饰者模式也称为包装模式(Wrapper Pattern),结构型模式之一,其使用一种对客户端透明的方式来动态的扩展对象的功能,同时它也是 ...

  10. 23种设计模式C++源码与UML实现--组合模式

    组合模式 Composite模式也叫做组合模式,是构造型的设计模式之一.通过递归的手段构造树形的对象结构,并可以通过一个对象来访问整个对象树. Component树形结构的节点抽象 为所有的对象定义统 ...

最新文章

  1. .net项目文档生成工具使用
  2. Linux之traceroute命令使用详解—网络故障定位(三)
  3. c语言计算坐标三角形面积公式,c语言计算三角形面积代码
  4. python中if语句and和or用法_python中if语句的使用(比较、逻辑、成员、运算符,语句结构)...
  5. 微服务 边界服务_遵循这些实用原则以获取精心设计的微服务边界
  6. python的selenium模块_Python中Selenium模块的使用
  7. [hadoop读书笔记] 第十五章 sqoop1.4.6小实验 - 将mysq数据导入hive
  8. 智能对联模型太难完成?华为云ModelArts助你实现!手把手教学
  9. 批量对word文档进行操作
  10. python 画图十大工具_python实现画图工具
  11. echart2文档(简单明白)
  12. docker的搭建和简单应用
  13. Linux文件锁内核VFS层源码实现讲解
  14. matlab中怎么贮存函数,MATLAB参数保存、调用
  15. (十二)Linux设置定时任务及开机自启动
  16. unity碰撞检测函数,碰撞信息获取,触发检测,使用粒子系统创建火焰,创建动画(火光闪烁),导航系统,通过导航系统控制人物移动,控制摄像机的跟随,控制角色动画播放
  17. 如何比较两个结构体是否相等
  18. java 之文件输入输出流
  19. JAVA-实现汽车租赁系统的计价功能
  20. 传导发射(CE)测试概述

热门文章

  1. Airbnb 面试题汇总
  2. Java多线程系列--“JUC线程池”01之 线程池架构
  3. [转]asp实现数据库导出excel文件
  4. 【转】char,wchar_t,WCHAR,TCHAR,ACHAR的区别
  5. 使用jstack和TDA进行java线程dump分析
  6. bookstore项目学到的对象
  7. xfce4快捷键设置
  8. 计算分词的Tf-idf值
  9. ORACLE 分页SQL
  10. Protocol Buffers动态消息解析