迭代器模式,主要用于容器里面元素的遍历,其他地方用到的不多,这里举个实例,模仿jdk里面迭代器的实现,当然我这里并没有把Iterator方法抽象到iterable这个接口中,依据接口的设计原则,不要做一个面面俱到的接口,而应该让特定的接口完成特定的功能
以下是代码示例,使用了之后,我们在测试代码中,仅仅只需要改动一个位置就可以实现不同list的切换
package com.zcj.designpattern;
import com.zcj.designpattern.*;
public class IteratorTest {
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
list.add("hello");
list.add("world");
System.out.println(list.size());
Iterator iterator =list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
接下来是我定义的List接口:
package com.zcj.designpattern;
//定义list的接口,采用泛型
public interface List<T> {
public int size();
public void add(T t);
public Iterator<T> iterator();
}
接着是Iterator接口的定义:
package com.zcj.designpattern;
public interface Iterator<T> {
public boolean hasNext();
public T next();
public void remove();
}
下面是二个List的实现:因为用到了链表,所以定义了一个Node类
package com.zcj.designpattern;
public class ArrayList<T> implements List<T>{
private T[] array = (T[]) new Object[10];
private int size=0;
@Override
public void add(T t) {
// TODO Auto-generated method stub
if(size>=array.length){
T[] array1 = (T[]) new Object[array.length*2];
for(int i=0;i<array1.length;i++){
array1[i]=array[i];
}
array=array1;
}
array[size]=t;
size++;
}
@Override
public int size() {
// TODO Auto-generated method stub
return size;
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return new ArrayListItrator();
}
private class ArrayListItrator implements Iterator<T>{
private int index=0;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return index<size;
}
@Override
public T next() {
// TODO Auto-generated method stub
return (T) array[index++];
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
}
package com.zcj.designpattern;
public class Node<T> {
private T val;
private Node next;
public Node(T val, Node next) {
super();
this.val = val;
this.next = next;
}
public T getVal() {
return val;
}
public void setVal(T val) {
this.val = val;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
package com.zcj.designpattern;
public class LinkedList<T> implements List<T> {
private Node headNode;
private Node tailNode;
private int size=0;
@Override
public void add(T t) {
if(headNode==null){
Node<T> node = new Node<T>(t, null);
headNode=node;
tailNode=node;
}
else{
Node<T> node = new Node<T>(t, null);
tailNode.setNext(node);
tailNode=node;
}
size++;
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return new LinkedListIterator();
}
@Override
public int size() {
// TODO Auto-generated method stub
return size;
}
private class LinkedListIterator implements Iterator<T> {
private Node pNode=headNode;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return pNode!=null;
}
@Override
public T next() {
// TODO Auto-generated method stub
T valT = (T) pNode.getVal();
pNode=pNode.getNext();
return valT;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
}
以上所有代码模拟了一个简单的迭代器模式的功能