使用频率:★★★★★
一、什么是迭代器模式
提供一种方法访问一个容器对象中各个元素,而又不需暴露该对象的内部细节。
二、补充说明
单一职责原则,分离了集合对象的遍历行为,抽象出一个迭代器类来负责;
三、角色
迭代器接口
迭代器具体实现
抽象容器
具体容器
四、例子,JAVA实现
例子说明:使用迭代器遍历容器
迭代器接口
package com.pichen.dp.behavioralpattern.iterator; public interface Iterator { public Object first(); public Object previous(); public Object next(); public boolean hasNext(); }
迭代器具体实现
package com.pichen.dp.behavioralpattern.iterator; import java.util.List; public class MyIterator implements Iterator{ private List<Object> list; private int index = 0; public MyIterator(List<Object> list) { this.list = list; } @Override public Object previous() { if((this.index - 1) < 0){ return null; }else{ return this.list.get(--index); } } @Override public Object next() { if((this.index + 1) >= this.list.size()){ return null; }else{ return this.list.get(++index); } } @Override public boolean hasNext() { if(this.index < (this.list.size() - 1)){ return true; } return false; } /** * 〈一句话功能简述〉 * 〈功能详细描述〉 * @see com.pichen.dp.behavioralpattern.iterator.Iterator#first() * @return */ @Override public Object first() { if(this.list.size() <= 0){ return null; }else{ return this.list.get(0); } } }
抽象容器
package com.pichen.dp.behavioralpattern.iterator; public abstract class Container { public abstract Iterator iterator(); public abstract void put(Object obj); }
具体容器
package com.pichen.dp.behavioralpattern.iterator; import java.util.ArrayList; import java.util.List; public class MyContainer extends Container{ private List<Object> list; public MyContainer() { this.list = new ArrayList<Object>(); } @Override public void put(Object obj){ this.list.add(obj); } @Override public Iterator iterator() { return new MyIterator(list); } }
客户端调用示例
package com.pichen.dp.behavioralpattern.iterator; public class Main { public static void main(String[] args) { //创建一个自定义容器,直接使用ArrayList的实现,仅仅示例作用 Container strContainer = new MyContainer(); strContainer.put("001"); strContainer.put("002"); strContainer.put("003"); Iterator myIterator = strContainer.iterator(); //使用举例 System.out.println("------------------next、hasNext示例------------------"); System.out.println(myIterator.first()); System.out.println(myIterator.next()); System.out.println(myIterator.hasNext()); System.out.println(myIterator.next()); System.out.println(myIterator.hasNext()); System.out.println(myIterator.next()); System.out.println(myIterator.hasNext()); //使用举例 System.out.println("------------------previous、hasNext示例------------------"); System.out.println(myIterator.previous()); System.out.println(myIterator.previous()); System.out.println(myIterator.previous()); System.out.println(myIterator.hasNext()); //使用迭代器遍历 System.out.println("------------------迭代器遍历示例------------------"); System.out.println(myIterator.first()); while(myIterator.hasNext()){ System.out.println(myIterator.next()); } } }
结果打印
------------------next、hasNext示例------------------ 001 002 true 003 false null false ------------------previous、hasNext示例------------------ 002 001 null true ------------------迭代器遍历示例------------------ 001 002 003