• 《JAVA与模式》之迭代器模式


    定义:提供一种方法访问一个容器对象中各个元素,而又不暴露该对象的内部细节。

    类型:行为类模式

    类图:

     1 public static void main(String[] args) {
     2         List<Object> list = new ArrayList<Object>();
     3         list.add("a");
     4         list.add("b");
     5         list.add("c");
     6         Aggregate aggregate = new ConcreteAggregate(list);
     7         Iterator iterator = aggregate.iterator();
     8 
     9         while (iterator.hasNext()) {
    10             String o = (String) iterator.next();
    11             System.out.println(o);
    12         }
    13     }
    14 
    15 }
    16 
    17 interface Iterator {
    18 
    19     public Object next();
    20 
    21     public boolean hasNext();
    22 
    23 }
    24 
    25 class ConcreteIterator implements Iterator {
    26     private List<Object> list;
    27     private int cursor = 0;// 当前游标位置
    28 
    29     public ConcreteIterator(List<Object> list) {
    30         this.list = list;
    31 
    32     }
    33 
    34     public boolean hasNext() {
    35         // TODO Auto-generated method stub
    36         return !(cursor == list.size());
    37     }
    38 
    39     public Object next() {
    40         // TODO Auto-generated method stub
    41         Object obj = null;
    42         if (hasNext()) {
    43             obj = list.get(cursor++);
    44         }
    45         return obj;
    46     }
    47 }
    48 
    49 // 模拟集合接口 增删 差(遍历)
    50 interface Aggregate {
    51 
    52     public void add(Object obj);
    53 
    54     public void remove(Object obj);
    55 
    56     public Iterator iterator();
    57 
    58 }
    59 
    60 class ConcreteAggregate implements Aggregate {
    61     private List<Object> list;
    62 
    63     public ConcreteAggregate(List<Object> list) {
    64         this.list = list;
    65 
    66     }
    67 
    68     public void add(Object obj) {
    69         list.add(obj);
    70 
    71     }
    72 
    73     public Iterator iterator() {
    74         // TODO Auto-generated method stub
    75         return new ConcreteIterator(list);
    76     }
    77 
    78     public void remove(Object obj) {
    79         list.remove(obj);
    80 
    81     }
    82 
    83 }

    迭代器模式的优缺点

            迭代器模式的优点有:

    • 简化了遍历方式,对于对象集合的遍历,还是比较麻烦的,对于数组或者有序列表,我们尚可以通过游标来取得,但用户需要在对集合了解很清楚的前提下,自行遍历对象,但是对于hash表来说,用户遍历起来就比较麻烦了。而引入了迭代器方法后,用户用起来就简单的多了。
    • 可以提供多种遍历方式,比如说对有序列表,我们可以根据需要提供正序遍历,倒序遍历两种迭代器,用户用起来只需要得到我们实现好的迭代器,就可以方便的对集合进行遍历了。
    • 封装性良好,用户只需要得到迭代器就可以遍历,而对于遍历算法则不用去关心。

            迭代器模式的缺点:

    • 对于比较简单的遍历(像数组或者有序列表),使用迭代器方式遍历较为繁琐,大家可能都有感觉,像ArrayList,我们宁可愿意使用for循环和get方法来遍历集合。

    迭代器模式的适用场景

           迭代器模式是与集合共生共死的,一般来说,我们只要实现一个集合,就需要同时提供这个集合的迭代器,就像java中的Collection,List、Set、Map等,这些集合都有自己的迭代器。假如我们要实现一个这样的新的容器,当然也需要引入迭代器模式,给我们的容器实现一个迭代器。

           但是,由于容器与迭代器的关系太密切了,所以大多数语言在实现容器的时候都给提供了迭代器,并且这些语言提供的容器和迭代器在绝大多数情况下就可以满足我们的需要,所以现在需要我们自己去实践迭代器模式的场景还是比较少见的,我们只需要使用语言中已有的容器和迭代器就可以了。

    参考地址:

    http://blog.csdn.net/chenhuade85/article/details/8146992

  • 相关阅读:
    微服务迁移记(二):注册中心(consul搭建)
    微服务迁移记(一):技术架构
    小程序入坑记录
    不思量,自难忘:我的10年程序生涯
    your password has expired.to log in you must change it
    Javascript中 a.href 和 a.getAttribute('href') 结果不完全一致
    PHP不使用递归的无限级分类
    百度移动搜索自动转码太坑爹,JS跳转地址会被抓取
    iScroll 下 a 标签失效
    浏览器的云加速可能导致IP统计异常
  • 原文地址:https://www.cnblogs.com/draem0507/p/3795189.html
Copyright © 2020-2023  润新知