设计模式21:Iterator Pattern (迭代器模式)

来源:互联网 发布:西安软件公司软件开发 编辑:程序博客网 时间:2024/05/20 20:22

可以查看英文原文:http://www.dofactory.com/Patterns/PatternIterator.aspx

一、迭代器模式:Iterator Pattern

Define:Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

定义:提供一种方式可以连续的访问几何对象的所有元素而无须关注内在的描述方式。

二、UML图

iterator

  • Iterator  (AbstractIterator)
    • 定义一个访问和操作元素的接口
  • ConcreteIterator  (Iterator)
    • 实现迭代器的接口
    • keeps track of the current position in the traversal of the aggregate.
  • Aggregate  (AbstractCollection)
    • 定义一个接口用于创建一个迭代器对象
  • ConcreteAggregate  (Collection)
    •  实现迭代器创建的接口并且返回一个合适的ConcreteIterator实例对象

    三、迭代器模式实例代码

     

    四、一个使用迭代器模式的例子

    该例子借助迭代器模式实现以下操作:迭代访问items的元素并且跳过每个迭代过程中items中指定的数字。

    原创粉丝点击