/** * @Title: Aggregate.java * @Package com.wangbiao.design * @Description: TODO * @author wangbiao * @date 2014-9-20 下午05:02:00 * @version V1.0 */ package com.wangbiao.design.iterator; import java.io.ObjectInputStream.GetField; import java.util.ArrayList; import java.util.List; /** * @Title: Aggregate.java * @Package com.wangbiao.design * @Description: TODO * @author wangbiao * @date 2014-9-20 下午05:02:00 * @version V1.0 */ public interface Aggregate { public abstract Object createIterator(); public abstract Object get(int index); } class ConcreteAggregate implements Aggregate{ public List<String> list = new ArrayList<String>(); @Override public Object createIterator() { return null; } @Override public Object get(int index) { if(index >= 0 ){ return list.get(index); } return null; } } /** * @Title: MyIterator.java * @Package com.wangbiao.design.iterator * @Description: TODO * @author wangbiao * @date 2014-9-20 下午05:09:02 * @version V1.0 */ package com.wangbiao.design.iterator; import java.util.Collection; import java.util.List; /** * @Title: MyIterator.java * @Package com.wangbiao.design.iterator * @Description: TODO * @author wangbiao * @date 2014-9-20 下午05:09:02 * @version V1.0 */ public interface MyIterator { public abstract Object first(); public abstract Object next(); public abstract boolean hasNext(); public abstract Object currentItem(); } class ConcreteMyIterator implements MyIterator{ public List<Object> list; public int conrrent = 0; public ConcreteMyIterator() { // TODO Auto-generated constructor stub } public ConcreteMyIterator( List<Object> list) { this.list = list; } @Override public Object first() { if(list != null){ return list.get(0); } return null; } @Override public Object next() { if(conrrent < list.size()){ return list.get(conrrent + 1); } return list.get(conrrent); } @Override public boolean hasNext() { if(conrrent >= list.size()){ return false; } return true; } @Override public Object currentItem() { return list.get(conrrent); } }