迭代器模式: 提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示
使用场景:
当需要访问一个聚集对象,而且不管这些对象是什么都需要遍历的时候,你就应该考虑用迭代器模式。
优点:
迭代器模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可以让外部代码透明的访问及和内部的数据。
UML图:
代码实现:
package com.liran.main.DesignPattern.迭代器模式;
import java.util.ArrayList;
import java.util.List;
/**
* 迭代器模式
* Created by LiRan on 2016-03-01.
*/
public class Iterator_pattern {
public static void main(String[] args) {
ConcreteAggregate aggregate=new ConcreteAggregate();
aggregate.set(0,"哈哈0");
aggregate.set(1,"哈哈1");
aggregate.set(2,"哈哈2");
aggregate.set(3,"哈哈3");
aggregate.set(4,"哈哈4");
aggregate.set(5,"哈哈5");
Iterator i=new ConcreteIterator(aggregate);
Object item=i.First();
while (!i.IsDone()){
System.out.println(i.CurrentItem()+"请买票");
i.Next();
}
}
}
/**
* Iterator迭代器抽象类
*/
abstract class Iterator {
public abstract Object First();
public abstract Object Next();
public abstract boolean IsDone();
public abstract Object CurrentItem();
}
/**
* Aggregate抽象聚集类
*/
abstract class Aggregate {
public abstract Iterator CreateIterator();
}
/**
* ConcreteIterator具体迭代器类,集成Iterator
*/
class ConcreteIterator extends Iterator {
private ConcreteAggregate aggregate;
private int current = 0;
public ConcreteIterator(ConcreteAggregate aggregate) {
this.aggregate = aggregate;
}
@Override
public Object First() {
return aggregate.get(0);
}
@Override
public Object Next() {
Object rect = null;
current++;
if (current < aggregate.count()) {
rect = aggregate.get(current);
}
return rect;
}
@Override
public boolean IsDone() {
return current >= aggregate.count() ? true : false;
}
@Override
public Object CurrentItem() {
return aggregate.get(current);
}
}
/**
* 具体聚集类
*/
class ConcreteAggregate extends Aggregate {
private List<Object> items = new ArrayList<>();
@Override
public Iterator CreateIterator() {
return new ConcreteIterator(this);
}
public int count() {
return items.size();
}
public Object get(int idex) {
return items.get(idex);
}
public void set(int idex, Object o) {
items.add(idex,o);
}
}