在Iterator Pattern中,我們實現(xiàn)了各式各樣的menu,有的menu中的元素的是數(shù)組,有的是ArrayList,有的是HashTable等等。我們用一個通用的Iterator 接口實現(xiàn)對各種類型menu的迭代調(diào)用。
但是有個問題,所有的這些menu可不可以有它的子菜單呢?比如說我的菜單里面有一種菜叫做“午餐套餐”,午餐套餐里面分為幾種,有法式的午餐套餐,有中式的套餐,還有俄式的,而“法式套餐”里面有5個菜,中式的有6個不同的菜。。。。。。
也就是說,能不能菜單“套”菜單呢?
Iterator Pattern只能提供一個平行的菜單組合,而我們要一個樹形的。
樹形的結(jié)點分為兩種類型:一種是葉節(jié)點,一種是非葉節(jié)點(包含子結(jié)點)。
我們給葉節(jié)點和非葉節(jié)點創(chuàng)造一個統(tǒng)一的接口MenuComponent方便調(diào)用(在調(diào)用的時候就并不需要知道該結(jié)點是非葉節(jié)點還是葉結(jié)點,這樣就可以對所有的結(jié)點進(jìn)行統(tǒng)一的操作),葉節(jié)點實現(xiàn)該接口的一部分功能(getName, getPrice, getDescription,print),而非葉節(jié)點實現(xiàn)另一部分功能(add, remove, getChildren,print),其中非葉節(jié)點的print是通過迭代其子結(jié)點的print功能實現(xiàn)的。屬于同一個父節(jié)點的各個子節(jié)點,我們用ArrayList來收集。
package javaapplication40;
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
MenuComponent mc1 = new Menu("menu0", "description0");
mc1.add(new MenuItem("menuItem1", "MenuDescription1", 2.9));
mc1.add(new MenuItem("menuItem2", "MenuDescription2", 3.9));
MenuComponent mc2 = new Menu("menu3", "description3");
mc1.add(mc2);
mc2.add(new MenuItem("menuItem4", "MenuDescription4", 4.9));
mc2.add(new MenuItem("menuItem5", "MenuDescription5", 5.9));
mc1.print();
}
}
interface MenuComponent {
public String getDescription();
public String getName();
public double getPrice();
public void print();
public void add(MenuComponent mc);
public void remove(MenuComponent mc);
public ArrayList<MenuComponent> getChildren();
}
class MenuItem implements MenuComponent { //leaf node
String name;
String description;
double price;
public MenuItem(String name, String description, double price) {
this.name = name;
this.description = description;
this.price = price;
}
public String getDescription() {
return description;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void print() {
System.out.println(getName() + ":" + getDescription() + " " + getPrice());
}
public void add(MenuComponent mc) { // 葉節(jié)點沒有以下三個功能
throw new UnsupportedOperationException("Not supported yet.");
}
public void remove(MenuComponent mc) {
throw new UnsupportedOperationException("Not supported yet.");
}
public ArrayList<MenuComponent> getChildren() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
class Menu implements MenuComponent { //非葉節(jié)點,包含子結(jié)點的結(jié)點
String name;
String description;
ArrayList<MenuComponent> children = new ArrayList<MenuComponent>();
public Menu(String name, String description) {
this.name = name;
this.description = description;
}
public void add(MenuComponent mc) {
children.add(mc);
}
public void remove(MenuComponent mc) {
children.remove(mc);
}
public ArrayList<MenuComponent> getChildren() {
return children;
}
public String getDescription() { //非葉節(jié)點沒有以下三個功能
throw new UnsupportedOperationException("Not supported yet.");
}
public String getName() {
throw new UnsupportedOperationException("Not supported yet.");
}
public double getPrice() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void print() { //兩種方法都可以,相當(dāng)于廣度搜索
// ArrayList<MenuComponent> children = new ArrayList<MenuComponent>();
// children = getChildren();
// for (MenuComponent mc : children) {
// mc.print();
// }
Iterator<MenuComponent> iterator = getChildren().iterator();//Iterator Pattern只能在一個平面上使用
while (iterator.hasNext()) {
iterator.next().print();
}
}
}