设计模式-迭代器模式


迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

public interface Aggregate<E> {
	Iterator<E> createIterator();
}

public class ConcreteAggregate implements Aggregate<Object> {

	Object[] object;
	
	public ConcreteAggregate(Object[] object) {
		this.object = object;
	}
	
	@Override
	public Iterator<Object> createIterator() {
		return new ConcreteIterator(this.object);
	}

}

public interface Iterator {
	boolean hasNext();
	void next();
	void remove();
}

public class ConcreteIterator implements java.util.Iterator {

	Object[] object;
	
	public ConcreteIterator(Object[] objects) {
		this.object = objects;
	}

	@Override
	public boolean hasNext() {
		return false;
	}

	@Override
	public Object next() {
		return null;
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		
	}

}

你可能感兴趣的:(设计模式)