合工大很牛很牛牛

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            14 Posts :: 1 Stories :: 37 Comments :: 0 Trackbacks
           

          Iterator Pattern中,我們實(shí)現(xiàn)了各式各樣的menu,有的menu中的元素的是數(shù)組,有的是ArrayList,有的是HashTable等等。我們用一個(gè)通用的Iterator 接口實(shí)現(xiàn)對(duì)各種類型menu的迭代調(diào)用。

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

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

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

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

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

                  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é)點(diǎn),包含子結(jié)點(diǎn)的結(jié)點(diǎn)

              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é)點(diǎn)沒有以下三個(gè)功能

                  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只能在一個(gè)平面上使用

                  while (iterator.hasNext()) {

                      iterator.next().print();

                  }

              }

          }

          posted on 2008-07-18 00:46 化的了 閱讀(1208) 評(píng)論(0)  編輯  收藏 所屬分類: 設(shè)計(jì)模式
          主站蜘蛛池模板: 房产| 兴安县| 察隅县| 中江县| 桐乡市| 山西省| 民权县| 卫辉市| 靖州| 综艺| 宕昌县| 平遥县| 通渭县| 文水县| 临夏县| 双峰县| 呼和浩特市| 托克逊县| 黄大仙区| 微博| 雷山县| 婺源县| 沙湾县| 响水县| 拉孜县| 丹东市| 古丈县| 眉山市| 墨江| 盘山县| 塔河县| 北流市| 苍溪县| 乌兰县| 贵德县| 万荣县| 辽阳县| 同仁县| 桑植县| 东乡县| 库伦旗|