设计模式之Interator模式

这个例子是模拟java jdk中的ArrayList及LinkedList的实现。

1.建一个ArrayList类,该类要实现可以向数组一样存储对象,并能够计算容器中元素的个数
package com.yx.zzg.iterator;

public class ArrayList{

	int index = 0;
	Object[] objects = new Object[10];

	public void add(Object o) {
		if (index == objects.length) {
			Object[] newObjects = new Object[objects.length * 2];
			System.arraycopy(objects, 0, newObjects, 0, objects.length);
			objects = newObjects;
		}
		objects[index] = o;
		index++;
	}

	public int size() {
		return index;
	}

}


2.建一个类LinkedList,实现像链表一样来存储对象
package com.yx.zzg.iterator;

public class LinkedList {

	Node head = null;

	Node tail = null;

	int size = 0;

	public void add(Object o) {
		Node n = new Node(o, null);
		if (head == null) {
			head = n;
			tail = n;
		} else {
			tail.setNext(n);
			tail = n;
		}
		size++;
	}

	public int size() {
		return size;
	}
}


3.编写测试类:
package com.yx.zzg.iterator;

public class Test {
	public static void main(String[] args) {
		//如果是LinkedList就new 一个LinkedList,因此可以抽取一个接口                   //(Collection)出来
                ArrayList c = new ArrayList();
		for (int i = 0; i < 15; i++) {
			c.add(new Cat(i));
		}
		System.out.println(c.size());
	}

}


4.抽取一个Collection接口,并让ArrayList和LinkedList都实现这个接口,调用的时候通过接口去调用。
package com.yx.zzg.iterator;

public interface Collection {
	
	public void add(Object o);
	
	public int size();

}


5.集合要遍历,但是两种容器的遍历方式不同(ArrayList是一种数组的方式,而LinkedList是一种链表的方式),为了达到有相同的遍历方式这种效果,可以定义一个接口(Iterator)
package com.yx.zzg.iterator;

public interface Iterator {

	boolean haseNext();

	Object next();

}


6.并且在Collection类中提供一个返回Iterator接口的方法iterator方法,因此最后Collection接口最后的定义为:
package com.yx.zzg.iterator;

public interface Collection {
	
	public void add(Object o);
	
	public int size();
	
	Iterator iterator();

}


7.因为ArrayList类和LinkedList类都实现了Collection接口,所以他们也必须实现iterator方法,所以ArrayList和LinkedList的最后定义为:
package com.yx.zzg.iterator;

public class ArrayList implements Collection {

	int index = 0;
	Object[] objects = new Object[10];

	public void add(Object o) {
		if (index == objects.length) {
			Object[] newObjects = new Object[objects.length * 2];
			System.arraycopy(objects, 0, newObjects, 0, objects.length);
			objects = newObjects;
		}
		objects[index] = o;
		index++;
	}

	public int size() {
		return index;
	}

	@Override
	public Iterator iterator() {
		return new ArrayListIterator();
	}
        //实现Iterator接口,提供遍历的具体实现
	private class ArrayListIterator implements Iterator {
		private int currentIndex = 0;

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

		@Override
		public Object next() {
			Object o = objects[currentIndex];
			currentIndex++;
			return o;
		}

	}

}


package com.yx.zzg.iterator;

public class LinkedList implements Collection {

	Node head = null;

	Node tail = null;

	int size = 0;

	public void add(Object o) {
		Node n = new Node(o, null);
		if (head == null) {
			head = n;
			tail = n;
		} else {
			tail.setNext(n);
			tail = n;
		}
		size++;
	}

	public int size() {
		return size;
	}

	@Override
	public Iterator iterator() {
		return new LinkedListIterator();
	}

	private class LinkedListIterator implements Iterator {

		@Override
		public boolean haseNext() {
			return false;
		}

		@Override
		public Object next() {
              return null;			
		}

	}

}

你可能感兴趣的:(java,设计模式,C++,c,C#)