就是一个迭代器,用来遍历集合。java 原生代码中的迭代器也是采用这种模式
1.程序设计
出于更深入的了解java迭代器原理,自己写一遍还是有必要的。
程序设计:两个接口,两个实现类。
集合接口:构造集合的存储结构
迭代器接口:用来对集合进行遍历。
自己定义一个User的model ,自定义集合,并定义添加item的方法,然后通过自己定义的迭代器,对其进行遍历。是不是很溜呀。那我们开始吧。这个弯子有点大,多看几遍就明白了。
集合接口: NotTrueCollection
package com.cn.iteractor; public interface NotTrueCollection<T> { NotTrueIteractor<T> createIteractor(); Object get(int i); int size(); void addItem(T t); void deleteItem(T t); }
迭代器接口: NotTrueIteractor
package com.cn.iteractor; public interface NotTrueIteractor<T> { boolean hasNext(); Object getPrevious(); Object next(); Object getFirst(); Object getLasst(); }
集合实现类:MyCollection
package com.cn.iteractor; public class MyCollection<T> implements NotTrueCollection<T> { private Object[] collections = new Object[1]; private int index = 0; @Override public NotTrueIteractor<T> createIteractor() { return new MyIteractor<T>(this); } @Override public Object get(int i) { return collections[i]; } @Override public int size() { return collections.length; } @Override public void addItem(T t) { int lengths = size(); if (index >= size()) { Object[] collectionTemp = collections; collections = new Object[lengths + 1]; for (int i = 0; i < collectionTemp.length; i++) { collections[i] = collectionTemp[i]; } collections[lengths] = t; } else { collections[lengths-1] = t; } index++; } @Override public void deleteItem(T t) { // TODO Auto-generated method stub } }
迭代器实现类 MyIteractorimplements
package com.cn.iteractor; public class MyIteractor<T> implements NotTrueIteractor<T> { private NotTrueCollection<T> notTrueCollectin; private int post = -1; public MyIteractor(NotTrueCollection<T> notTrueCollectin) { this.notTrueCollectin = notTrueCollectin; } @Override public boolean hasNext() { if (post < notTrueCollectin.size() - 1) { return true; } return false; } @Override public Object getPrevious() { if (post > 0) { post--; } return notTrueCollectin.get(post); } @Override public Object next() { if (post < notTrueCollectin.size() - 1) { post++; } return notTrueCollectin.get(post); } @Override public Object getFirst() { return notTrueCollectin.get(0); } @Override public Object getLasst() { return notTrueCollectin.get(notTrueCollectin.size() - 1); } }
测试类: User
package com.cn.iteractor; public class User { private String name; private String sex; private int age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [name=" + name + ", sex=" + sex + ", age=" + age + ", address=" + address + "]"; } }
测试结果:Test
package com.cn.iteractor;
public class Test {
public static void main(String[] args) {
NotTrueCollection<User> myCollection = new MyCollection<>();
User user1 = new User();
User user2 = new User();
User user3 = new User();
user1.setName("Harvey");
user2.setName("Tom");
user3.setName("Jack");
myCollection.addItem(user1);
myCollection.addItem(user2);
myCollection.addItem(user3);
NotTrueIteractor<User> iteractor = myCollection.createIteractor();
while (iteractor.hasNext()) {
User users = (User) iteractor.next();
System.out.println(users);
}
}
}
说明:代码中User模型省略了getset还有toString()方法。另外遍历似乎只有在遍历集合的时候才需要,但是java 原生的代码已经将集合遍历完美的实现,我们只要理解其原理就行了。