行为型模式--迭代子模式(Iterator)

顾名思义,迭代器模式就是顺序访问聚集中的对象,一般来说,集合中非常常见,如果对集合类比较熟悉的话,理解本模式会十分轻松。这句话包含两层意思:一是需要遍历的对象,即聚集对象,二是迭代器对象,用于对聚集对象进行遍历访问。我们看下关系图:

 

这个思路和我们常用的一模一样,MyCollection中定义了集合的一些操作,MyIterator中定义了一系列迭代操作,且持有Collection实例,我们来看看实现代码:

两个接口:

[java]  view plain copy
  1. public interface Collection {  
  2.       
  3.     public Iterator iterator();  
  4.       
  5.     /*取得集合元素*/  
  6.     public Object get(int i);  
  7.       
  8.     /*取得集合大小*/  
  9.     public int size();  
  10. }  
[java]  view plain copy
  1. public interface Iterator {  
  2.     //前移  
  3.     public Object previous();  
  4.       
  5.     //后移  
  6.     public Object next();  
  7.     public boolean hasNext();  
  8.       
  9.     //取得第一个元素  
  10.     public Object first();  
  11. }  

两个实现:

[java]  view plain copy
  1. public class MyCollection implements Collection {  
  2.   
  3.     public String string[] = {"A","B","C","D","E"};  
  4.     @Override  
  5.     public Iterator iterator() {  
  6.         return new MyIterator(this);  
  7.     }  
  8.   
  9.     @Override  
  10.     public Object get(int i) {  
  11.         return string[i];  
  12.     }  
  13.   
  14.     @Override  
  15.     public int size() {  
  16.         return string.length;  
  17.     }  
  18. }  
[java]  view plain copy
  1. public class MyIterator implements Iterator {  
  2.   
  3.     private Collection collection;  
  4.     private int pos = -1;  
  5.       
  6.     public MyIterator(Collection collection){  
  7.         this.collection = collection;  
  8.     }  
  9.       
  10.     @Override  
  11.     public Object previous() {  
  12.         if(pos > 0){  
  13.             pos--;  
  14.         }  
  15.         return collection.get(pos);  
  16.     }  
  17.   
  18.     @Override  
  19.     public Object next() {  
  20.         if(pos<collection.size()-1){  
  21.             pos++;  
  22.         }  
  23.         return collection.get(pos);  
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean hasNext() {  
  28.         if(pos<collection.size()-1){  
  29.             return true;  
  30.         }else{  
  31.             return false;  
  32.         }  
  33.     }  
  34.   
  35.     @Override  
  36.     public Object first() {  
  37.         pos = 0;  
  38.         return collection.get(pos);  
  39.     }  
  40.   
  41. }  

测试类:

[java]  view plain copy
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) {  
  4.         Collection collection = new MyCollection();  
  5.         Iterator it = collection.iterator();  
  6.           
  7.         while(it.hasNext()){  
  8.             System.out.println(it.next());  
  9.         }  
  10.     }  
  11. }  

输出:A B C D E

此处我们貌似模拟了一个集合类的过程,感觉是不是很爽?其实JDK中各个类也都是这些基本的东西,加一些设计模式,再加一些优化放到一起的,只要我们把这些东西学会了,掌握好了,我们也可以写出自己的集合类,甚至框架!

==================================================================

下面看迭代子的另外一个例子:

package my;


public class Test {

	public static void main(String[] args) {

		Stack s = new Stack();

		s.push("Liucy");

		s.push("Huxz");

		s.push("George");
		print(s.iterator());

		LinkedList l = new LinkedList();

		l.addFirst("Liucy");

		l.addFirst("Huxz");

		l.addFirst("George");

		print(l.iterator());

	}

	public static void print(Itr it) {

		while (it.hasNext()) {

			System.out.println(it.next());

		}

	}

}

interface Itr {

	boolean hasNext();

	Object next();

}

class Stack {

	Object[] os = new Object[10];

	int index = 0;

	private void expand() {

		Object[] os2 = new Object[os.length * 2];

		System.arraycopy(os, 0, os2, 0, os.length);

		os = os2;

	}

	public void push(Object o) {

		if (index == os.length)
			expand();

		os[index] = o;

		index++;

	}

	public Object pop() {

		index--;

		Object o = os[index];

		os[index] = null;

		return o;

	}

	private class StackItr implements Itr {

		int cursor = 0;

		public boolean hasNext() {

			return cursor < index;
		}

		public Object next() {

			return os[cursor++];

		}

	}

	public Itr iterator() {

		return new StackItr();

	}

}

class LinkedList {

	private class Node {

		Object o;

		Node next;

		public Node(Object o) {

			this.o = o;

		}

		public void setNext(Node next) {

			this.next = next;

		}

		public Node getNext() {

			return this.next;

		}

	}

	Node head;

	public void addFirst(Object o) {

		Node n = new Node(o);

		n.setNext(head);

		head = n;

	}

	public Object removeFirst() {

		Node n = head;

		head = head.getNext();

		return n.o;

	}

	class LinkedListItr implements Itr {

		Node currentNode = head;

		public boolean hasNext() {

			return this.currentNode != null;

		}

		public Object next() {

			Node n = currentNode;

			currentNode = currentNode.getNext();

			return n.o;

		}

	}

	public Itr iterator() {

		return new LinkedListItr();

	}

}

 ===============================================================
2、

 抽象聚集角色类,这个角色规定出所有的具体聚集必须实现的接口。迭代子模式要求聚集对象必须有一个工厂方法,也就是createIterator()方法,以向外界提供迭代子对象的实例。

复制代码
public abstract class Aggregate {
    /**
     * 工厂方法,创建相应迭代子对象的接口
     */
    public abstract Iterator createIterator();
}
复制代码

  具体聚集角色类,实现了抽象聚集角色类所要求的接口,也就是createIterator()方法。此外,还有方法getElement()向外界提供聚集元素,而方法size()向外界提供聚集的大小等。

复制代码
public class ConcreteAggregate extends Aggregate {
    
    private Object[] objArray = null;
    /**
     * 构造方法,传入聚合对象的具体内容
     */
    public ConcreteAggregate(Object[] objArray){
        this.objArray = objArray;
    }
    
    @Override
    public Iterator createIterator() {
        
        return new ConcreteIterator(this);
    }
    /**
     * 取值方法:向外界提供聚集元素
     */
    public Object getElement(int index){
        
        if(index < objArray.length){
            return objArray[index];
        }else{
            return null;
        }
    }
    /**
     * 取值方法:向外界提供聚集的大小
     */
    public int size(){
        return objArray.length;
    }
}
复制代码

  抽象迭代子角色类

复制代码
public interface Iterator {
    /**
     * 迭代方法:移动到第一个元素
     */
    public void first();
    /**
     * 迭代方法:移动到下一个元素
     */
    public void next();
    /**
     * 迭代方法:是否为最后一个元素
     */
    public boolean isDone();
    /**
     * 迭代方法:返还当前元素
     */
    public Object currentItem();
}
复制代码

  具体迭代子角色类

复制代码
public class ConcreteIterator implements Iterator {
    //持有被迭代的具体的聚合对象
    private ConcreteAggregate agg;
    //内部索引,记录当前迭代到的索引位置
    private int index = 0;
    //记录当前聚集对象的大小
    private int size = 0;
    
    public ConcreteIterator(ConcreteAggregate agg){
        this.agg = agg;
        this.size = agg.size();
        index = 0;
    }
    /**
     * 迭代方法:返还当前元素
     */
    @Override
    public Object currentItem() {
        return agg.getElement(index);
    }
    /**
     * 迭代方法:移动到第一个元素
     */
    @Override
    public void first() {
        
        index = 0;
    }
    /**
     * 迭代方法:是否为最后一个元素
     */
    @Override
    public boolean isDone() {
        return (index >= size);
    }
    /**
     * 迭代方法:移动到下一个元素
     */
    @Override
    public void next() {

        if(index < size)
        {
            index ++;
        }
    }

}
复制代码

  客户端类

复制代码
public class Client {

    public void operation(){
        Object[] objArray = {"One","Two","Three","Four","Five","Six"};
        //创建聚合对象
        Aggregate agg = new ConcreteAggregate(objArray);
        //循环输出聚合对象中的值
        Iterator it = agg.createIterator();
        while(!it.isDone()){
            System.out.println(it.currentItem());
            it.next();
        }
    }
    public static void main(String[] args) {
        
        Client client = new Client();
        client.operation();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值