合工大很牛很牛牛

            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 化的了 閱讀(1215) 評論(0)  編輯  收藏 所屬分類: 設計模式
          主站蜘蛛池模板: 乌海市| 平和县| 武隆县| 明星| 宽城| 山东| 永新县| 定襄县| 开远市| 武清区| 盐城市| 扶绥县| 新野县| 南部县| 安西县| 黄浦区| 新安县| 焦作市| 利川市| 万州区| 子洲县| 和田市| 永靖县| 临西县| 香格里拉县| 寻乌县| 诸暨市| 香港| 陈巴尔虎旗| 临沂市| 和林格尔县| 乡宁县| 杂多县| 麟游县| 大宁县| 江都市| 鄂州市| 理塘县| 琼海市| 和顺县| 永州市|