合工大很牛很牛牛

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            14 Posts :: 1 Stories :: 37 Comments :: 0 Trackbacks
           

          Iterator Pattern中,我們實現了各式各樣的menu,有的menu中的元素的是數組,有的是ArrayList,有的是HashTable等等。我們用一個通用的Iterator 接口實現對各種類型menu的迭代調用。

          但是有個問題,所有的這些menu可不可以有它的子菜單呢?比如說我的菜單里面有一種菜叫做“午餐套餐”,午餐套餐里面分為幾種,有法式的午餐套餐,有中式的套餐,還有俄式的,而“法式套餐”里面有5個菜,中式的有6個不同的菜。。。。。。

          也就是說,能不能菜單“套”菜單呢?

          Iterator Pattern只能提供一個平行的菜單組合,而我們要一個樹形的。

          樹形的結點分為兩種類型:一種是葉節點,一種是非葉節點(包含子結點)。

          我們給葉節點和非葉節點創造一個統一的接口MenuComponent方便調用(在調用的時候就并不需要知道該結點是非葉節點還是葉結點,這樣就可以對所有的結點進行統一的操作),葉節點實現該接口的一部分功能(getName, getPrice, getDescription,print),而非葉節點實現另一部分功能(add, remove, getChildren,print),其中非葉節點的print是通過迭代其子結點的print功能實現的。屬于同一個父節點的各個子節點,我們用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) {  // 葉節點沒有以下三個功能

                  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 { //非葉節點,包含子結點的結點

              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() { //非葉節點沒有以下三個功能

                  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() { //兩種方法都可以,相當于廣度搜索

          //        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();

                  }

              }

          }

          posted on 2008-07-18 00:46 化的了 閱讀(1214) 評論(0)  編輯  收藏 所屬分類: 設計模式
          主站蜘蛛池模板: 拉孜县| 肥乡县| 南通市| 宁都县| 宿松县| 永靖县| 峨眉山市| 荥阳市| 施甸县| 浪卡子县| 七台河市| 云和县| 固始县| 梧州市| 綦江县| 天门市| 株洲市| 沙洋县| 庆安县| 珲春市| 曲沃县| 德钦县| 五指山市| 南康市| 清涧县| 哈尔滨市| 临汾市| 兴宁市| 新闻| 合江县| 沙雅县| 南安市| 崇信县| 德惠市| 同德县| 离岛区| 东乡| 陈巴尔虎旗| 伽师县| 崇明县| 尚志市|