head first---------composite design pattern (二)

2014-11-24 10:14:31 · 作者: · 浏览: 1
}
public boolean isVegetarian(){
throw new UnsupportedOperationException();
}
//all the time to use by menu and menuItem
public void print(){
throw new UnsupportedOperationException();
}
public abstract Iterator createIterator();
}
package com.clark.compositpattern;
import java.util.Iterator;
/**
* MenuItem extends MenuComponent
* @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;
}
//print method
public void print(){
System.out.println(" "+getName());
if(isVegetarian()){
System.out.print(" "+vegetarian);
}
System.out.println(","+getPrice());
System.out.println(" -----"+getDescription());
}
@Override
public Iterator createIterator() {
return new NullIterator();
}
}
package com.clark.compositpattern;
/**
* Test class
*
* @author Administrator
*
*/
public class MenuTestDrive {
public static void main(String[] args) {
// first make up MenuComponent component
MenuComponent pancakeHouseMenu = new Menu("Pancake House Menu",
"Breakfast");
MenuComponent dinerMenu = new Menu("Diner Menu", "Lunch");
MenuComponent cafeMenu = new Menu("Cafe Menu", "Diner");
MenuComponent dessertMenu = new Menu("Dessert Menu",
"Dessert of Course!");
MenuComponent coffeeMenu = new Menu("COFFEE MENU",
"Stuff to go with your afternoon coffee");
// create a new all Menu
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(