chapter09_管理良好的集合——迭代器与组合模式

  • 迭代器可以让客户遍历集合但无法窥视内部对象存储的方式

  • (1) 不使用迭代器会让遍历操作无法统一接口

      public static void printMenu() {
    
          PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
          DinerMenu dinerMenu = new DinerMenu();
    
          ArrayList<MenuItem> breakfastItems = pancakeHouseMenu.getMenuItems();
          MenuItem[] lunchItems = dinerMenu.getMenuItems();
    
          // Exposing implementation
          System.out.println("USING FOR LOOPS");
      
          for (int i = 0; i < breakfastItems.size(); i++) {
    
              MenuItem menuItem = (MenuItem) breakfastItems.get(i);
              System.out.print(menuItem.getName());
              System.out.println("\t\t" + menuItem.getPrice());
              System.out.println("\t" + menuItem.getDescription());
          }
    
          for (int i = 0; i < lunchItems.length; i++) {
    
              MenuItem menuItem = lunchItems[i];
              System.out.print(menuItem.getName());
              System.out.println("\t\t" + menuItem.getPrice());
              System.out.println("\t" + menuItem.getDescription());
          }
      }
    

    (2) 使用迭代器便于统一接口

      public void printMenu() {
    
          Iterator pancakeIterator = pancakeHouseMenu.createIterator();
          Iterator dinerIterator = dinerMenu.createIterator();
    
          System.out.println("MENU\n----\nBREAKFAST");
          printMenuHelper(pancakeIterator);
    
          System.out.println("\nLUNCH");
          printMenuHelper(dinerIterator);
      }
    
      private void printMenuHelper(Iterator iterator) {
    
          while (iterator.hasNext()) {
    
              MenuItem menuItem = iterator.next();
          
              System.out.print(menuItem.getName() + ", ");
              System.out.print(menuItem.getPrice() + " -- ");
              System.out.println(menuItem.getDescription());
          }
      }
    
  • 迭代器中一般包含两个方法

      public interface Iterator {
    
          boolean hasNext();
    
          MenuItem next();
      }
    

    一种内部是数组的迭代器的实现

      public class DinerMenuIterator implements Iterator {
    
          private MenuItem[] items;
          private int position = 0;
    
          public DinerMenuIterator(MenuItem[] items) {
    
              this.items = items;
          }
    
          public MenuItem next() {
    
              MenuItem menuItem = items[position];
              position = position + 1;
      
              return menuItem;
          }
    
          public boolean hasNext() {
    
              if (position >= items.length || items[position] == null) {
                  return false;
              } else {
                  return true;
              }
          }
      }
    

    这样, 只需在遍历集合前创建对应的迭代器即可

      public Iterator createIterator() {
          return new DinerMenuIterator(menuItems);
      }
    
  • Java本身提供了Iterator接口

      public interface Iterator<E> {
    
          boolean hasNext();
    
          E next();
    
          default void remove() {
              throw new UnsupportedOperationException("remove");
          }
    
          default void forEachRemaining(Consumer<? super E> action) {
       
              Objects.requireNonNull(action);
       
              while (hasNext())
                  action.accept(next());
          }
      }
    

    因此, Java的集合类一般都提供了产生迭代器的方法

    ArrayList.java

      public class ArrayList<E> extends AbstractList<E>
      implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
    
          ...
    
          public Iterator<E> iterator() {
              return new Itr();
          }
    
          ...
    
      }
    

    Iterator()方法中, 产生了一个ArrayList的内部类的对象Itr

      public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
    
          ...
    
          private class Itr implements Iterator<E> {
    
              ...
          }
    
          ...
      }
    
  • 迭代器模式

    提供一种方法顺序访问一个聚合对象中的各个元素, 而又不暴露其内部的表示

  • 设计原则: 单一责任原则

    一个类应该只有一个引起变化的原因

    例如在没有使用迭代器以前, 一个集合类既要维持内部的数据结构, 又要提供遍历访问操作, 就有了多重的责任; 现在使用迭代器将遍历的责任交给其他的类


  • (1) 当集合中的对象内部又嵌套了集合对象时, 在不使用任何设计模式的情况下, 会导致在遍历集合的时候, 需要单独判断当前对象的类型再做操作

    (2) 为了解决(1)中的问题, 可以定义一个超类, 让单个对象和对象集合都继承这个超类;

    在超类中定义抽象方法, 然后由单个对象的类和对象集合类实现;

    这样, 整个就会组织成一个树形结构, 可以容纳菜单(超类)、子菜单(对象集合)、菜单项(单个对象)

    (3) 示例

    MenuComponent.java

      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 String getDescription() {
    
              throw new UnsupportedOperationException();
          }
    
          public double getPrice() {
    
              throw new UnsupportedOperationException();
          }
    
          public boolean isVegetarian() {
    
              throw new UnsupportedOperationException();
          }
    
          public void print() {
    
              throw new UnsupportedOperationException();
          }
      }
    

    MenuComponent是一个超类, 定义了一系列默认方法的实现, 这些实现中都会抛出UnsupportedOperationException异常;

    MenuItem.java

      public class MenuItem extends MenuComponent {
    
          private String name;
          private String description;
          private boolean vegetarian;
          private 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());
          }
      }
    

    MenuItem是具体的组件类, 它实现了它可以实现的方法, 调用它未实现的方法就会调用它的基类的方法从而抛出异常

    Menu.java

      public class Menu extends MenuComponent {
    
          private ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
          private String name;
          private 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 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<MenuComponent> iterator = menuComponents.iterator();
    
              while (iterator.hasNext()) {
                  MenuComponent menuComponent = iterator.next();
                  menuComponent.print();
              }
          }
      }
    

    Menu是菜单类, 它内部包含一个存储MenuComponent的集合, 在print()方法中, 会调用内部MenuComponent的print()方法

  • 组合模式

    将对象组合成树形结构来表现整体/部分层次结构。 组合可以让客户以一致的方式处理个别对象以及对象组合

  • (1) 组合模式保证了透明性: 客户在操作是将组合对象和叶结点一视同仁, 一个元素是组合对象还是叶结点对客户是透明

    (2) 保证透明性的同时产生了安全性问题, 因为调用时很可能会调用抛出UnsupportedException的方法。 解决方法是将组合对象和叶结点定义成不同接口, 用instanceof判断;

    但是, 这样又丧失了透明性.

    所以, 组合模式就是透明性和安全性的折中

    (3) 为啥要把组合模式和迭代器模式放在一起呢?

    因为组合模式在需要遍历集合的时候, 也会使用迭代器

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,695评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,569评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,130评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,648评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,655评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,268评论 1 309
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,835评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,740评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,286评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,375评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,505评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,873评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,357评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,466评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,921评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,515评论 2 359

推荐阅读更多精彩内容