当有数个对象的集合,它们彼此之间有“整体/部分”的关系,并且你想用一致的方法对待这些对象,就需要用到组合模式。
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();
}
abstract Iterator createIterator();
}
public class Menu extends MenuComponent {
ArrayList menuComponents = new ArrayList();
String name;
String description;
public Menu(String name, String description) {
super();
this.name = name;
this.description = description;
}
@Override
public void add(MenuComponent component) {
menuComponents.add(component);
}
@Override
public void remove(MenuComponent component) {
menuComponents.remove(component);
}
@Override
public MenuComponent getChild(int i) {
return (MenuComponent) menuComponents.get(i);
}
@Override
public String getName() {
return name;
}
@Override
public String getDescription() {
return description;
}
@Override
public void print() {
System.out.print("\n" + getName());
System.out.println(", " + getDescription());
System.out.println("-----------------------");
Iterator iterator = menuComponents.iterator();
while (iterator.hasNext()) {
MenuComponent component = (MenuComponent) iterator.next();
component.print();
}
}
@Override
Iterator createIterator() {
return new CompositeIterator(menuComponents.iterator());
}
}
public class MenuItem extends MenuComponent {
String name;
String description;
boolean vegetarian;
double price;
public MenuItem(String name, String description, boolean vegetarian,
double price) {
super();
this.name = name;
this.description = description;
this.vegetarian = vegetarian;
this.price = price;
}
@Override
public String getName() {
return name;
}
@Override
public String getDescription() {
return description;
}
@Override
public double getPrice() {
return price;
}
@Override
public boolean isVegetarian() {
return vegetarian;
}
public void print() {
System.out.print(" " + getName());
if (isVegetarian()) {
System.out.println("(v)");
}
System.out.println(", " + getPrice());
System.out.println(" --" + getDescription());
}
@Override
Iterator createIterator() {
return new NullIterator();
}
}
public class CompositeIterator implements Iterator {
Stack<Iterator> stack = new Stack<Iterator>();
public CompositeIterator(Iterator iterator) {
stack.add(iterator);
}
@Override
public boolean hasNext() {
if (stack.empty()) {
return false;
} else {
Iterator iterator = stack.peek();
if (!iterator.hasNext()) {
stack.pop();
return hasNext();
} else
return true;
}
}
@Override
public Object next() {
if (hasNext()) {
Iterator iterator = stack.peek();
MenuComponent component = (MenuComponent) iterator.next();
if (component instanceof Menu) {
stack.push(component.createIterator());
}
return component;
} else
return null;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public class NullIterator implements Iterator {
@Override
public boolean hasNext() {
return false;
}
@Override
public Object next() {
return null;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public class Waitress {
MenuComponent allMenus;
public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
}
public void printMenu() {
this.allMenus.print();
}
public void printVegetarianMenu() {
Iterator iterator = allMenus.createIterator();
while (iterator.hasNext()) {
MenuComponent component = (MenuComponent) iterator.next();
try {
if (component.isVegetarian()) {
component.print();
}
} catch (UnsupportedOperationException e) {
e.printStackTrace();
}
}
}
}
public class MenuTestDrive {
public static void main(String[] args) {
MenuComponent pancakeHouseMenu =
new Menu("PANCAKE HOUSE MENU", "Breakfase");
MenuComponent dinerMenu =
new Menu("DINER MENU", "Lunch");
MenuComponent cafeMenu =
new Menu("CAFE", "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);
dinerMenu.add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce", true, 3.89));
dinerMenu.add(dessertMenu);
dessertMenu.add(new MenuItem("Apple Pie", "Apple pie with a flakey crust", true, 1.99));
Waitress waitress = new Waitress(allMenus);
waitress.printMenu();
}
}