`
lisa.zhou
  • 浏览: 3753 次
  • 性别: Icon_minigender_2
最近访客 更多访客>>
社区版块
存档分类
最新评论

设计模式之Iterator

 
阅读更多


 迭代器模式:目前该模式已被整入到Java的Collection。
适用于:
  • 访问一个聚合对象的内容而不暴露它的内部表示
  • 支持聚合对象的多种遍历
  • 为遍历不同的聚合结构提供统一的接口
下边用代码模拟Java的Iterator:
  1. 定义Iterator接口,定义迭代接口方法
    /*
     * imitate the JDK Iterator
     */
    public interface Iterator<E> {
    	public E next();
    	public boolean hasNext();
    }
  2. 定义Collection接口,及集合的通用方法如 添加,获取遍历,获得总实体数
    /**
     * @author lisa
     * imitate the Collection of JDK
     */
    public interface Collection<E> {
    	public void add(E e);
    	public int size();
    	public Iterator iterator();
    }
  3. 分别定义集合类 ArrayList和LinkedList类,实现Collection接口,实现具体的方法
    /**
     * @author lisa
     * imitate the Collection of JDK
     */
    public interface Collection<E> {
    	public void add(E e);
    	public int size();
    	public Iterator iterator();
    }
    
     
    public class LinkedList<E> implements Collection<E> {
    	private Node head = null;
    	private Node tail = null;
    	private int size;
    	
    	@Override
    	public void add(E e) {
    		Node n = new Node(e,null);
    		if(head == null){
    			head = n;
    			tail = n;
    		}else{
    			tail.setNext(n);
    			tail = n;	
    		}
    			
    		size++;
    	}
    
    	@Override
    	public int size() {
    		// TODO Auto-generated method stub
    		return size;
    	}
    
    	@Override
    	public Iterator iterator() {
    		return new LinkedListIterator();
    	}
    	
    	private class LinkedListIterator implements Iterator {
    		private int currentIndex;
    		@Override
    		public boolean hasNext() {
    			if(currentIndex < size){
    				return true;
    			}
    			return false;
    		}
    
    		@Override
    		public Object next() {
    			Node node = head;
    			for(int i=1;i<=currentIndex;i++){
    				node = node.getNext();
    			}
    			currentIndex++;
    			return node.getData();
    		}
    	}
    
    }
    public class Node {
    	private Object data;
    	private Node next;
    	
    	public Node(Object data, Node next) {
    		super();
    		this.data = data;
    		this.next = next;
    	}
    	public Object getData() {
    		return data;
    	}
    	public void setData(Object data) {
    		this.data = data;
    	}
    	public Node getNext() {
    		return next;
    	}
    	public void setNext(Node next) {
    		this.next = next;
    	}	
    }
     
     注意LinkedList的思想:head标示链表列的头部,tail标示最后一个节点,每当有节点加入时,将当前的tail的next指向新节点,并将新节点作为新的tail
  4. 定义实体类
    public class Cat {
    	private int id;
    
    	public Cat(int id) {
    		super();
    		this.id = id;
    	}
    
    	@Override
    	public String toString() {
    		return "Cat: id=" +id;
    	}
    	
    }
    
     
  5. 定义测试类,对ArrayList进行操作和LinkedList
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		ArrayList<Cat> catList = new ArrayList<Cat>();
    		
    		
    		//LinkedList catList = new LinkedList<Cat>();
    		for(int i=1; i<=15; i++){
    			catList.add(new Cat(i));
    		}
    		System.out.println(catList.size());
    		Iterator iter = catList.iterator();
    		while(iter.hasNext()){
    			System.out.println(iter.next());
    		}
    	}
    
    }
    
     
  • 大小: 117.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics