一模式定义
迭代器模式,提供了一种模式顺序访问一个集合对象中各个元素的功能,而又不暴露其内部的表示。
二模式举例
1模式分析
我们借用访问人员列表这一案例来说明这一模式。
2迭代器模式静态类图

3代码示例
3.1 人员信息接口——IPerson
- package com.demo.person;
-
- /**
- * 人员信息
- *
- * @author
- *
- */
- public interface IPerson
- {
- /**
- * 获得人员信息
- *
- * @return
- */
- public String getPersonInfo();
-
- }
3.2 人员信息实现——Person
- package com.demo.person;
-
- /**
- * 人员具体实现类
- *
- * @author
- *
- */
- public class Person implements IPerson
- {
- // 姓名
- private String name;
- // 年龄
- private int age;
- // 性别(1:男性 0:女性)
- private int sex;
-
- /**
- * 构造方法设置属性内容
- *
- * @param name
- * @param age
- * @param sex
- */
- public Person(String name, int age, int sex)
- {
- this.name = name;
- this.age = age;
- this.sex = sex;
- }
-
- /**
- * 获得人员信息
- *
- * @return
- */
- public String getPersonInfo()
- {
- return "姓名:" + this.name + " - 年龄:" + this.age + " - 性别:" + (this.sex == 1 ? "男" : (this.sex == 0 ? "女" : ""));
- }
-
- }
3.3 人员集合接口——IPersonList
- package com.demo.person;
-
- import com.demo.iterator.Iterator;
-
- /**
- * 人员接口
- *
- * @author
- *
- */
- public interface IPersonList {
- /**
- * 获得内部存储人员信息内容
- *
- * @return
- */
-
- public IPerson[] getPersonList();
-
- /**
- * 迭代器
- *
- * @return
- */
- public Iterator iterator();
- }
3.4 人员集合实现——PersonList
- package com.demo.person;
-
- import java.util.Random;
-
- import com.demo.iterator.ArrPersonIterator;
- import com.demo.iterator.Iterator;
-
- /**
- * 人员列表信息
- *
- * @author
- *
- */
- public class PersonList implements IPersonList {
- // 存储用户信息列表
- private final IPerson[] list = new IPerson[10];
-
- // 构造方法初始化人员信息
- public PersonList() {
- Random random = new Random();
- // 创建人员信息
- for (int i = 0; i < 10; i++) {
- IPerson person = new Person("人员" + i, random.nextInt(30), random
- .nextInt(2));
- list[i] = person;
- // this.list.add(person);
- }
- }
-
- /**
- * 获得内部存储人员信息内容
- *
- * @return
- */
-
- public IPerson[] getPersonList() {
- return list;
- }
-
- /**
- * 迭代器
- *
- * @return
- */
- public Iterator iterator() {
- return new ArrPersonIterator(this.list);
- }
- }
3.5 迭代器接口——Iterator
- package com.demo.iterator;
-
- /**
- * 迭代器接口
- *
- * @author
- *
- */
- public interface Iterator {
-
- // 判断是否含有下一个届节点
- public boolean hasNext();
-
- // 获得下一个节点对象
- public Object next();
-
- // 删除对象
- public Object remove();
- }
3.6 迭代器实现——ArrPersonIterator
- package com.demo.iterator;
-
- import com.demo.person.IPerson;
-
- /**
- * 数组迭代器实现
- *
- * @author
- *
- */
- public class ArrPersonIterator implements Iterator {
- // 私有属性存储人员列表对象信息
- private final IPerson[] personList;
- // 存储位置信息 初始值为-1
- private int index = -1;
-
- /**
- * 构造方法将人员列表对象传入
- *
- * @param personList
- */
- public ArrPersonIterator(IPerson[] personList) {
- this.personList = personList;
- }
-
- // 判断是否含有下一个节点
- public boolean hasNext() {
- return (this.personList == null ? false
- : (index < this.personList.length - 1));
- }
-
- // 获得下一个节点对象
- public Object next() {
- if (this.personList != null && (index < this.personList.length - 1)) {
- // 获得人员列表对象中的人员信息
- return this.personList[++index];
- }
- return null;
- }
-
- // 删除对象
- public Object remove() {
- if (this.personList != null) {
- IPerson person = this.personList[index];
- this.personList[index] = null;
- return person;
- }
-
- return null;
- }
-
- }
3.7 让迭代器遍历集合对象——Client
- package com.demo;
-
- import com.demo.iterator.Iterator;
- import com.demo.person.IPerson;
- import com.demo.person.IPersonList;
- import com.demo.person.PersonList;
-
- public class Client {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 创建人员列表对象
- IPersonList personList = new PersonList();
- System.out.println("----使用迭代器输出人员信息 start......");
- // 生成迭代器
- Iterator iterator = personList.iterator();
- // 循环迭代器 遍历每一个元素输出人员信息
- while (iterator.hasNext()) {
- // 获得人员对象实例
- IPerson person = (IPerson) iterator.next();
- if (person != null) {
- // 输出人员信息
- System.out.println(person.getPersonInfo());
- }
- }
- System.out.println("----使用迭代器输出人员信息 end......");
-
- }
- }
4运行结果
----使用迭代器输出人员信息 start......
姓名:人员0 - 年龄:28 - 性别:女
姓名:人员1 - 年龄:25 - 性别:女
姓名:人员2 - 年龄:23 - 性别:女
姓名:人员3 - 年龄:18 - 性别:女
姓名:人员4 - 年龄:27 - 性别:女
姓名:人员5 - 年龄:28 - 性别:男
姓名:人员6 - 年龄:25 - 性别:女
姓名:人员7 - 年龄:23 - 性别:女
姓名:人员8 - 年龄:16 - 性别:男
姓名:人员9 - 年龄:12 - 性别:女
----使用迭代器输出人员信息 end......
三该模式设计原则
1"开一闭"原则
2单一职责原则
四使用场合
1访问一个集合对象的内容,而无须暴露它的内部表示。
2支持对集合对象的多种遍历方式。
3为遍历不同的集合对象结构提供一个统一的接口。
五迭代器模式静态类图

|