设计模式—迭代器Iterator模式

什么是迭代器模式?

让用户通过特定的接口访问容器的数据,不需要了解容器内部的数据结构。

 

首先我们先模仿集合中ArrayList和LinkedList的实现。一个是基于数组的实现、一个是基于链表的实现,实现方式各有不同,

为了减少代码的耦合度,面向接口编程。定义Collection接口定义API规范。

可是在遍历集合中的元素时,由于数组和链表的遍历方式不一样,能不能统一处理呢?

再定义一个Iterator接口,让每一个子类集合都实现接口中的方法。

1.接口Collection.java

public interface Collection<E> {
    public void add(E e);
    public int size();
    public Iterator iterator();
}

2.接口Iterator.java

public interface Iterator {
    public Object next();
    public boolean hasNext();
}

 

3.实现ArrayList.java  

public class ArrayList<E> implements Collection<E>{
 
    //先给定一个长度为10的数组
    Object [] objects = new Object[10];
     
    //冗余一个int指数,方便判定是组是否为满和返回集合大小
    int index = 0;
 
    @Override
    //1.动态添加元素
    public void add(E e) {
         
        //1.1先判断数组是否已满
        if(index == objects.length){
            Object [] newObjects = new Object[objects.length*2];
            System.arraycopy(objects, 0, newObjects, 0, objects.length);
            objects = newObjects;   //数组是引用数据类型
        }
         
        //1.2为新添加的元素指定下标
        objects[index] = e;
         
        //1.3index自加1,以方便返回集合在大小
        index++;
    }
 
    //2.根据下标访问元素
     
    @Override
    //3.返回集合大小
    public int size() {
        return index;
    }
 
    @Override
    public Iterator iterator() {
        return new ArrayListIterator();
    }
     
    private class ArrayListIterator implements Iterator {
 
        private int currentIndex = 0;
         
        @Override
        public Object next() {
            // 返回最下一个元素
            Object o = objects[currentIndex];
            currentIndex++;
            return o;
        }
 
        @Override
        public boolean hasNext() {
            // 判断是否为最后一个元素
             
            if(currentIndex >= index){
                return false;
            }
            return true;
        }
         
    }
     
}

4.Node.java

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;
    }
     
     
}
View Code

5.实现LinkedList.java

public class LinkedList<e> implements Collection<e> {
 
    private Node head;
    private Node tail;
    private int size;
     
    public void add(E e){
        Node n = new Node(e, null);
        if(head == null){
            head = n;
            tail = n;
            size++;
        } else {
            tail.setNext(n);
            tail = n;
            size++;
        }
    }
     
    public int size(){
        return size;
    }
 
    @Override
    public Iterator iterator() {
        return new LinkedListIterator();
    }
     
    private class LinkedListIterator implements Iterator {
 
        private Node currentNode = head;
         
        @Override
        public Object next() {
            Object o = currentNode.getData();
            currentNode = currentNode.getNext();
            return o;
        }
 
        @Override
        public boolean hasNext() {
            if(currentNode.getNext() == null){
                return false;
            }
            return true;
        }
         
    }
}

 

在以上的实现可关注:1.ArrayList和LinkedList的不同实现方式

          2.Iterator模式的运用

          3.实现Iterator时内部类的使用

 

posted @ 2017-05-16 21:30  开拖拉机的蜡笔小新  阅读(364)  评论(0编辑  收藏  举报