JAVA之迭代器模式easy come easy go!
2007-09-03 11:44
288 查看
迭代器模式是一种大家经常要用到的模式,在JAVA的Collections中大家可以经常应用
最常见的到的是下面这种程序结构
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
(Iterator i = Object.hasnext;i.next;)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// to do something;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
在用这个模式的时候,我们通常就调用这里的方法,而不关心它的内部组织过程,所以让我们来看看它的内在东东。
这里我先给出一个例子。
这个例子是我随便想的,说的是一个山上没有任何的树,现在我们要种植几个树,例如 苹果树什么的,然后我要遍历这些树,给每颗树喷洒不同的农药!
在这个例子中我们可以看到,遍历树的话可以用到迭代器模式!
首先我们要做个迭代器接口,为什么要抽象出来一个接口,(这个问题大家自己网上找接口的好处)!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public interface Iterator ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public boolean hasNext();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Object next();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这个接口我们有2个方法,hasNext()是否还有下一条数据,next返回具体的Object 这里也就是树。
我们先不必要忙着做他的实现类,我们现在要来做的是这个容器(不是JAVA中容器,与arraylist什么的无关),正所谓树的容器是什么,是山也!
我们想想山应该具有什么呢!?
首先它要有种植树的功能,这里可以看作添加树。我们可以想像山的功能是和树相互关联的,那么他们之间是什么关系呢,我们给他们一
种聚合的关系,聚合的关系大家可以参考UML图,我在这里给出它的一种程序表现形式。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Hall ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Tree[] tree ; //这里可以看作是聚合关系
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int index; //指向Tree[]的标签
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Hall(int maxNumber) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tree = new Tree[maxNumber];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
index = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void add(Tree tree)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.tree[index]=tree;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
index++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Iterator connectIterator()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new TreeIterator(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里我们定义的山可以抽象出Hall类来,Tree[] tree 可以看作是山和树之间的一种聚合关系。add方法就是添加树。问题来了,山
和树有了关系,那么山和迭代器有什么关系呢。它们之间肯定有一种关系。我们有了这个容器(山),就要把这个容器来实现迭代的方法:hasNext()和Next().恩这里我们可以看
出,山和迭代器之间也是一种关联关系。我们就把它看成是一种聚合关系(TIP:聚合关系一种特殊的关联关系)。我们可以通过一个connectIterator方法来链接山和迭代器,接下来
我们要去做一个具体的迭代类,这个具体的类中间有了hasNext()和Next()的具体实现方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class TreeIterator implements Iterator...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int last=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Hall hall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public TreeIterator(Hall hall) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.hall=hall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public boolean hasNext()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(last<hall.tree.length)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Tree next()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Tree t = hall.tree[last];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
last++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return t;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里Hall hall 就可以看作是一种关联关系,我们要把山和迭代器联系起来就通过构造函数来实现,hasNext和next实现方法就体现出来了
有了山,有了迭代器,可是树还没有定义,不过这个树的方法还很好解决!树不关联其他的事务,我们可以简单的这么写:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Tree ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Tree(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name=name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
好了似乎我们整个工程完工了,我们现在来模拟一下农民老大伯来种树撒肥的过程;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Pren ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Pren() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hall hall = new Hall(4);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hall.add(new Tree("苹果树"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hall.add(new Tree("梨树"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hall.add(new Tree("橘子树"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hall.add(new Tree("凤梨树"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(Iterator i = hall. connectIterator();i.hasNext();)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String Type = ((Tree)(i.next())).getName();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(Type=="苹果树")...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("洒苹果树的农药,");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(Type=="梨树")...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("洒梨树的农药");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(Type=="橘子树")...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("洒橘子树的农药,洒了也没用,还没到成熟日,现在没结果实");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(Type=="凤梨树")...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("南风天,湿气大,让它烂在地里吧");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
农民的英文单词不记得怎么写了,好像记得是P开头的 ,这里就用Pren 来代替了,首先农民老大伯要有种树的地方,也就是山的实例:Hall hall = new Hall(4); 这座山呢只能
种4个树,山小而五脏俱全,更像一个土包,再要有树才行啊 ,所以4个树的实例出现。好了接下来种树,几毫秒解决!山了有我们就要把山放到迭代器中间去了。遍历这个山(容
器)。
联想
我们看看arrayList中的迭代器模式是怎么实现的!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ArrayList a = new ArrayList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.add("a1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.add("a2");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.add("a3");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.add("a4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(Iterator i= a.iterator();i.hasNext();)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(i.next().toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
是不是很多地方和我们地例子相似!
以上呢是我举出的一个例子,不过这个程序还有很多的不完善的地方!标准的设计模式中呢还有一个Aggregae接口,容器还有实现这个接口来获得具体的迭代器!大家可以参考
JAVA设计模式,我这里没有添加这个接口!还有人要问干脆不做个数组就行了吗!?
呵呵 这个问题大家自己考虑,想想为什么要Vector 为什么要ArrayList;
最常见的到的是下面这种程序结构
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
(Iterator i = Object.hasnext;i.next;)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// to do something;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
在用这个模式的时候,我们通常就调用这里的方法,而不关心它的内部组织过程,所以让我们来看看它的内在东东。
这里我先给出一个例子。
这个例子是我随便想的,说的是一个山上没有任何的树,现在我们要种植几个树,例如 苹果树什么的,然后我要遍历这些树,给每颗树喷洒不同的农药!
在这个例子中我们可以看到,遍历树的话可以用到迭代器模式!
首先我们要做个迭代器接口,为什么要抽象出来一个接口,(这个问题大家自己网上找接口的好处)!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public interface Iterator ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public boolean hasNext();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Object next();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这个接口我们有2个方法,hasNext()是否还有下一条数据,next返回具体的Object 这里也就是树。
我们先不必要忙着做他的实现类,我们现在要来做的是这个容器(不是JAVA中容器,与arraylist什么的无关),正所谓树的容器是什么,是山也!
我们想想山应该具有什么呢!?
首先它要有种植树的功能,这里可以看作添加树。我们可以想像山的功能是和树相互关联的,那么他们之间是什么关系呢,我们给他们一
种聚合的关系,聚合的关系大家可以参考UML图,我在这里给出它的一种程序表现形式。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Hall ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Tree[] tree ; //这里可以看作是聚合关系
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int index; //指向Tree[]的标签
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Hall(int maxNumber) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tree = new Tree[maxNumber];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
index = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void add(Tree tree)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.tree[index]=tree;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
index++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public Iterator connectIterator()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new TreeIterator(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里我们定义的山可以抽象出Hall类来,Tree[] tree 可以看作是山和树之间的一种聚合关系。add方法就是添加树。问题来了,山
和树有了关系,那么山和迭代器有什么关系呢。它们之间肯定有一种关系。我们有了这个容器(山),就要把这个容器来实现迭代的方法:hasNext()和Next().恩这里我们可以看
出,山和迭代器之间也是一种关联关系。我们就把它看成是一种聚合关系(TIP:聚合关系一种特殊的关联关系)。我们可以通过一个connectIterator方法来链接山和迭代器,接下来
我们要去做一个具体的迭代类,这个具体的类中间有了hasNext()和Next()的具体实现方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class TreeIterator implements Iterator...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private int last=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Hall hall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public TreeIterator(Hall hall) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.hall=hall;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public boolean hasNext()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(last<hall.tree.length)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Tree next()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Tree t = hall.tree[last];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
last++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return t;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里Hall hall 就可以看作是一种关联关系,我们要把山和迭代器联系起来就通过构造函数来实现,hasNext和next实现方法就体现出来了
有了山,有了迭代器,可是树还没有定义,不过这个树的方法还很好解决!树不关联其他的事务,我们可以简单的这么写:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Tree ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Tree(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name=name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
好了似乎我们整个工程完工了,我们现在来模拟一下农民老大伯来种树撒肥的过程;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package TreeIterator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Pren ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Pren() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Hall hall = new Hall(4);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hall.add(new Tree("苹果树"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hall.add(new Tree("梨树"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hall.add(new Tree("橘子树"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hall.add(new Tree("凤梨树"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(Iterator i = hall. connectIterator();i.hasNext();)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String Type = ((Tree)(i.next())).getName();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(Type=="苹果树")...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("洒苹果树的农药,");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(Type=="梨树")...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("洒梨树的农药");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(Type=="橘子树")...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("洒橘子树的农药,洒了也没用,还没到成熟日,现在没结果实");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(Type=="凤梨树")...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("南风天,湿气大,让它烂在地里吧");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
农民的英文单词不记得怎么写了,好像记得是P开头的 ,这里就用Pren 来代替了,首先农民老大伯要有种树的地方,也就是山的实例:Hall hall = new Hall(4); 这座山呢只能
种4个树,山小而五脏俱全,更像一个土包,再要有树才行啊 ,所以4个树的实例出现。好了接下来种树,几毫秒解决!山了有我们就要把山放到迭代器中间去了。遍历这个山(容
器)。
联想
我们看看arrayList中的迭代器模式是怎么实现的!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ArrayList a = new ArrayList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.add("a1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.add("a2");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.add("a3");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a.add("a4");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
for(Iterator i= a.iterator();i.hasNext();)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(i.next().toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
是不是很多地方和我们地例子相似!
以上呢是我举出的一个例子,不过这个程序还有很多的不完善的地方!标准的设计模式中呢还有一个Aggregae接口,容器还有实现这个接口来获得具体的迭代器!大家可以参考
JAVA设计模式,我这里没有添加这个接口!还有人要问干脆不做个数组就行了吗!?
呵呵 这个问题大家自己考虑,想想为什么要Vector 为什么要ArrayList;
相关文章推荐
- HDU2132 An easy problem 【Java】
- java/android 设计模式学习笔记(20)---迭代器模式
- 发布我的 Rest 风格的 MVC 框架 EasyPage for Java
- 详解java设计模式(四)之迭代器模式(行为型)
- java easyreport 导入excel、 txt 数据数据格式校验(六)
- [LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)
- Java迭代器模式
- java设计模式之迭代器模式
- 迭代器模式(Java与IOS)
- JAVA设计模式六--Iterator(迭代器模式)
- leetcode 637. Average of Levels in Binary Tree(java easy)
- java设计模式16_迭代器模式
- java设计模式_迭代器模式
- python,java,go语言之间的区别!
- 设计模式之迭代器模式java实现代码
- Java/Go实现——快速排序
- java设计模式——迭代器模式
- 4种编程语言基本数据类型及其取值范围整理(C++,Java,Python,Go)
- Web Tier to Go With Java EE 5: A Look at Resource Injection
- 从 Java 到 Go,再到 Java