设计模式之迭代器模式
Posted 独孤九戒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式之迭代器模式相关的知识,希望对你有一定的参考价值。
设计模式之迭代器模式
迭代器模式(ITerator Parttern)是目前已经没落的一个设计模式,定义如下:Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.(它提供一种方法访问一个容器对象中的各个元素,而又不需要暴露该对象内部的细节)
通用类图
四个角色
1.Iterator抽象迭代器,抽象迭代器负责定义访问和遍历元素的接口,基本上有三个固定的方法:first()获取第一个元素,next()访问下一个元素,isDone()是否已经访问到底部;
2.ConcreteIterator具体迭代器,具体迭代器角色实现迭代器接口,完成容器元素的遍历;
3.Aggregate抽象容器,容器角色负责提供创建具体迭代器角色的接口,必然有一个类似createIterator()这样的方法;
4.ConcreteAggregate具体容器,具体容器实现容器接口定义的方法,创建出容纳迭代器的对象。
通用源码
抽象迭代器
public interface Iterator
public Object next();
public boolean hasNext();
public boolean remove();
具体迭代器
public class ConcreteIterator implements Iterator
private Vector vector=new Vector();
public int cursor=0;
public ConcreteIterator(Vector _vector)
this.vector=_vector;
public boolean hasNext()
if(this.cursor==this.vector.size())
return false;
else
return true;
public Object next()
Object result=null;
if(this.hasNext())
return this.vector.get(this.cursor++);
else
result=null;
return result;
public boolean remove()
this.vector.remove(this.cursor);
return true;
抽象容器
public interface Aggregate
public void add(Object object);
public void remove(Object object);
public Iterator iterator();
具体容器
public class ConcreteAggregate implements Aggregate
private Vector vector=new Vector();
public void add(Object object)
this.vector.add(object);
public Iterator iterator()
return new ConcreteIterator(this.vector);
public void remove(Object object)
this.remove(object);
场景类
public class Client
public static void main(String[] args)
// TODO Auto-generated method stub
Aggregate agg=new ConcreteAggregate();
agg.add("abc");
agg.add("aaa");
agg.add("1234");
Iterator iterator=agg.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
例子:整理项目信息
项目信息接口
public interface IProject
public void add(String name,int num,int cost);
public String getProjectInfo();
public IProjectIterator iterator();
项目信息
public class Project implements IProject
private ArrayList<IProject> projectList=new ArrayList<IProject>();
private String name="";
private int num=0;
private int cost=0;
public Project()
public Project(String name,int num,int cost)
this.name=name;
this.num=num;
this.cost=cost;
public void add(String name,int num,int cost)
this.projectList.add(new Project(name,num,cost));
public String getProjectInfo()
String info="";
info=info+"项目名称:"+this.name;
info=info+"\\t项目人数:"+this.num;
info=info+"\\t项目费用"+this.cost;
return info;
public IProjectIterator iterator()
return new ProjectIterator(this.projectList);
项目迭代器接口
public interface IProjectIterator extends Iterator
项目迭代器
public class ProjectIterator implements IProjectIterator
private ArrayList<IProject> projectList=new ArrayList<IProject>();
private int currentItem=0;
public ProjectIterator(ArrayList<IProject> projectList)
this.projectList=projectList;
public boolean hasNext()
boolean b=true;
if(this.currentItem>=projectList.size()||this.projectList.get(this.currentItem)==null)
b=false;
return b;
public IProject next()
return (IProject)this.projectList.get(this.currentItem++);
public void remove()
客户端
public class Client
public static void main(String[] args)
// TODO Auto-generated method stub
IProject project=new Project();
project.add("星球大战项目", 10, 100000);
project.add("扭转时空项目", 100, 1000000);
project.add("超人改造项目", 1000, 1000000);
for(int i=0;i<104;++i)
project.add("第"+i+"个项目", i*5, i*10000);
IProjectIterator projectIterator=project.iterator();
while(projectIterator.hasNext())
IProject p=(IProject)projectIterator.next();
System.out.println(p.getProjectInfo());
以上是关于设计模式之迭代器模式的主要内容,如果未能解决你的问题,请参考以下文章