组合模式类图如下:
实现菜单组件
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 double getPrice() {
throw new UnsupportedOperationException();
}
public boolean isVegetarian() {
throw new UnsupportedOperationException();
}
public void print() {
throw new UnsupportedOperationException();
}
}
实现菜单项
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 double getPrice() {
return price;
}
public boolean isVegetarian() {
return vegetarian;
}
public void print() {
System.out.print(" "+getName());
if(isVegetarian()) {
System.out.print("(v)");
}
System.out.println(","+getPrice());
System.out.println(" --"+getDescription());
}
}
实现组合菜单
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<E> iterator=menuComponents.iterator();
while(iterator.hasNext()) {
MenuComponent menuComponent=(MenuComponent)iterator.next();
menuComponent.print();
}
}
Waitress的实现如下:
public class Waitress{
MenuComponent allMenus;
public Waitress(MenuComponent allMenus) {
this.allMenus=allMenus;
}
public void printMenu() {
allMenus.print();
}
}
编写测试程序:
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 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,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 ice cream",true,1.59));
Waitress waitress=new Waitress(allMenus);
waitress.printMenu();
}
}