java设计模式---迭代器模式

2017-08-28  本文已影响0人  shuaidong

迭代器模式

java迭代器模式,也叫java iterator模式,下面举个以模仿java的collecton、ArrayList、LinckedList为例

需要建立的类关系图

1

案例代码

定义一个collection接口

/**
 * Created by td on 2017/8/28.
 * java迭代器模式,也叫java iterator模式。
 * 举个实例 模仿java的collecton、ArrayList、LinckedList
 */
public interface Collection<E> {
    public void add(E e);
    public int size();
    public Iterator iterator();
}

实现ArrayList


import java.util.*;

/**
 * Created by td on 2017/8/28.
 */
public class ArratList<E> implements Collection<E> {
    //先给定一个长度为10的数组
    Object [] objects = new Object[10];

    //冗余一个数组,方便判断数组是否满了和集合大小
    int  index = 0;

    //添加元素
    @Override
    public void add(E e) {
        //说明数组满了
        if (index==objects.length){
            Object [] newObjects = new Object[objects.length*2];
            System.arraycopy(objects,0,newObjects,0,objects.length);
            objects = newObjects;
        }
        objects[index] = e;
        index++;

    }

    @Override
    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() {
            E e = (E) objects[currentIndex];
            currentIndex++;
            return e;
        }

        @Override
        public boolean hasNext() {
            if (currentIndex>=index) {
                return false;
            }
            return true;
        }
    }
}

实现LinkedList


import javax.swing.table.TableCellEditor;
import java.util.*;

/**
 * Created by td on 2017/8/28.
 */
public class LinkedList<E> implements Collection<E>{
    private Node head;
    private Node tail;
    private  int size;

    @Override
    public void add(E e) {
        Node node = new Node(e,null);
        if (head==null){
            head = node;
            tail = node;
            size++;
        }else {
            tail.setNext(node);
            tail = node;
            size++;
        }

    }

    @Override
    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() {
            currentNode = currentNode.getNext();
          return currentNode.getData();
        }

        @Override
        public boolean hasNext() {
            if (currentNode.getNext()==null){
                return false;
            }
            return true;
        }
    }
}

定义Node

/**
 * Created by td on 2017/8/28.
 */
public class Node {
    private Object data;
    private Node next;

    public Node(Object data, Node next) {
        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;
    }
}

定义迭代器接口


/**
 * Created by td on 2017/8/28.
 */
public interface Iterator {
    public Object next();
    public boolean hasNext();
}

集合中装的对象,用于测试迭代器

/**
 * Created by td on 2017/8/28.
 */
public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

定义迭代器测试类,进行测试

/**
 * Created by td on 2017/8/28.
 */
public class CollectionTest {
    public static void main(String[] args) {
        Collection<Person> collection = new LinkedList<Person>();
        for (int i=0;i<10;i++){
            Person person = new Person(i+"name",i);
            collection.add(person);
        }
        System.out.println(collection.size());

        Iterator iterator = collection.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
        System.out.println("=========================");

        collection = new ArratList<Person>();
        for (int i=0;i<10;i++){
            Person person = new Person(i+"name",i);
            collection.add(person);
        }
        iterator =  collection.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
}

测试结果

10
Person{name='1name', age=1}
Person{name='2name', age=2}
Person{name='3name', age=3}
Person{name='4name', age=4}
Person{name='5name', age=5}
Person{name='6name', age=6}
Person{name='7name', age=7}
Person{name='8name', age=8}
Person{name='9name', age=9}
=========================
Person{name='0name', age=0}
Person{name='1name', age=1}
Person{name='2name', age=2}
Person{name='3name', age=3}
Person{name='4name', age=4}
Person{name='5name', age=5}
Person{name='6name', age=6}
Person{name='7name', age=7}
Person{name='8name', age=8}
Person{name='9name', age=9}
上一篇下一篇

猜你喜欢

热点阅读