迭代器模式在java的集合中比较常用,遍历对象比较方便,在内部封装内部方法, 访问一个聚合对象的内容而无须暴露它的内部表示, 需要为聚合对象提供多种遍历方式,为遍历不同的聚合结构提供一个统一的接口。
提供一种方法顺序访问一个聚合对象中各个元素, 而又无须暴露该对象的内部表示。
不同的方式来遍历整个整合对象。
定义接口:hasNext, next
/**
* 用户接口
*/
public interface IUser {
//获取用户信息
String getUserInfo();
}
package behavior.IteratorPattern;
public class User implements IUser{
private String userName;
private Integer userAge;
private String userAddr;
private String userDept;
public User() {
}
public User(String userName, Integer userAge, String userAddr, String userDept) {
this.userName = userName;
this.userAge = userAge;
this.userAddr = userAddr;
this.userDept = userDept;
}
//...省略 set get方法
@Override
public String getUserInfo() {
return "User[" +
"userName='" + userName + '\'' +
", userAge=" + userAge +
", userAddr='" + userAddr + '\'' +
", userDept='" + userDept + '\'' +
']';
}
}
public class UserDemo {
public static void main(String[] args) {
//定义user集合
ArrayList<IUser> userList = new ArrayList<>();
//添加元素
userList.add(new User("tony",12,"beijing","开发部"));
userList.add(new User("marry",16,"shanghai","人力部"));
userList.add(new User("tom",18,"beijing","开发部"));
userList.add(new User("tylor",14,"shenzhen","财务部"));
//遍历
for(IUser user:userList){
System.out.println(user.getUserInfo());
}
}
}
打印输出:
User[userName='tony', userAge=12, userAddr='beijing', userDept='开发部']
User[userName='marry', userAge=16, userAddr='shanghai', userDept='人力部']
User[userName='tom', userAge=18, userAddr='beijing', userDept='开发部']
User[userName='tylor', userAge=14, userAddr='shenzhen', userDept='财务部']
/**
* 提供一个迭代器接口
*/
public interface Iterator {
boolean hasNext();
Object next();
}
package behavior.IteratorPattern;
import java.util.ArrayList;
/**
* 用户迭代器
*/
public class UserIterator implements Iterator{
//用户列表
ArrayList<IUser> userList = new ArrayList<>();
//索引位置
int currentIndex = 0 ;
//构造函数
public UserIterator(ArrayList<IUser> userList) {
this.userList = userList;
}
@Override
public boolean hasNext() {
if(currentIndex >= userList.size() ||
userList.get(currentIndex) == null){
return false;
}
return true;
}
@Override
public IUser next() {
return this.userList.get(currentIndex++);
}
}
增加添加用户以及获取迭代器接口
//添加用户信息
void add(String userName, Integer userAge, String userAddr, String userDept );
//迭代器
UserIterator getIterator();
新增一个内部用户列表,实现新增的方法
private ArrayList<IUser> userList = new ArrayList<>();
@Override
public void add(String userName, Integer userAge, String userAddr, String userDept) {
this.userList.add(new User(userName,userAge,userAddr,userDept));
}
@Override
public UserIterator getIterator() {
return new UserIterator(this.userList);
}
package behavior.IteratorPattern;
/**
*
*/
public class IteratorDemoPattern {
public static void main(String[] args) {
IUser user = new User();
user.add("tony",12,"beijing","开发部");
user.add("marry",16,"shanghai","人力部");
user.add("tom",18,"beijing","开发部");
user.add("tylor",14,"shenzhen","财务部");
//遍历一下ArrayList,把所有的数据都取出
UserIterator userIterator = user.getIterator();
while(userIterator.hasNext()){
IUser u = (IUser)userIterator.next();
System.out.println(u.getUserInfo());
}
}
}
打印输出:
User[userName='tony', userAge=12, userAddr='beijing', userDept='开发部']
User[userName='marry', userAge=16, userAddr='shanghai', userDept='人力部']
User[userName='tom', userAge=18, userAddr='beijing', userDept='开发部']
User[userName='tylor', userAge=14, userAddr='shenzhen', userDept='财务部']
具体可以查看java.util.Iterator的源码,接口Collection实现public interface Collection extends Iterable 。Set, List, Map, SortedSet, SortedMap, HashSet, TreeSet, ArrayList, LinkedList, Vector, Collections, Arrays, AbstractCollection。