【设计模式】之迭代器(Iterator)
迭代器的定义为:提供一个无需暴露对象的底层实现而连续访问聚合对象元素的方法。
Provide a way to access the elements of an aggregate objectsequentially withoutexposing its underlying representation。
由定义可以看出,Iterator有三个关键词,首先是聚合对象(aggregate object),《Head First》中给出了定义:
When we say COLLECTION we just mean a group of objects. They might be stored in very different data structures lik
e lists, arrays, hashtables, but they're still collections. We also sometimes call these AGGREGATE.
简单来说一个容器就是一个聚合对象。
第二个关键词是连续地(sequentially),迭代器要能够追踪当前要访问的元素。
第三个关键词是暴露(exposing),迭代器隐藏了对象的聚合方式,也就是说使用迭代器的客户是无法知道对象的聚合方式是数组还是列表。
迭代器模式的关键是遵循了单一责任(Single Responsibility)的设计原则:
The key idea in this pattern is to take the responsibility for access and traversal out of the list object and put it into aniterator object. --<Design Patterns>
Assign each responsibility to one class and only one class. --<Head First Design Patterns>
单一责任可以让iterator更专注地去完成访问元素的任务,因此它可以在不该变客户端代码的前提下,实现各种各样的访问方式,并且提供了一个统一的访问方式。
迭代器可以看作是聚合对象的扩展。
An iterator can be viewed as an extension of the aggregate that created it.
迭代器的内容太多了,还分为external与internal。external就是需要客户端去迭代,而internal则只需要调用一个方法即可,迭代过程都是对客户端是透明的。
举例说明
首先定义一个迭代器的接口 Interator
public class MenuTestDrive { public static void main(String[] args) { PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); DinerMenu dinerMenu = new DinerMenu(); Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu); waitress.printMenu(); }}
通过代码可以看出,客户端代码Waitress的代码,不许要关心每个菜单对象内部的聚合情况,只需要调用相应的遍历器便可。
下一节组合模式(Composite)里面会继续用到迭代器。