Design Pattern (8)-iterator pattern (iterator)
I started to write this blog last Saturday, and it was delayed. I started writing this blog only a few days ago. I woke up early today and sorted this part out. This article describes how to learn the design mode and the iterator mode of the horse soldiers.
Understanding the iterator mode makes it more natural to use the iterator to traverse collection classes.
1. Introduction to iterator Mode
[Definition] The Fall object mode provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
[Principle] consists of four parts: iterator role, specific iterator role, container role, and specific container role.
[Time to use] when accessing the content of an aggregate object, you do not need to expose its internal representation, or you need to support multiple traversal of the collection object, or
When traversing different aggregate structures to provide a unified interface, you can consider using the iterator mode.
2. Implementation of ArrayList and aggregate list iterators
Here is a simple implementation. For details, refer to the source code. I remember that one of the blogs in csdn, Lanting, wrote a collection-type source code parsing article.
Code:
Iterator. java
package com.chan;public interface Iterator {Object next();boolean hasNext();}
Collection. java
package com.chan;public interface Collection {void add(Object obj);int size();Iterator iterator();}
Node, java
package com.chan;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;}}
ArrayList. java
package com.chan;public class ArrayList implements Collection {Object[] objects = new Object[10];int index = 0;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;}public Iterator iterator() {return new ArrayListIterator();}private class ArrayListIterator implements Iterator {private int currentIndex = 0;@Overridepublic boolean hasNext() {if(currentIndex >= index) return false;else return true;}@Overridepublic Object next() {Object o = objects[currentIndex];currentIndex ++;return o;}}}
Using list. java
Package com. chan; public class collections List 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;} tail. setNext (n); tail = n; size ++;} public int size () {return size ;}@ Overridepublic Iterator iterator () {return new initialize listiterator ();} private class initialize listiterator implements Iterator {private No De currentNode = head; private int nextIndex = 0; // refer to @ Overridepublic Object next () {Object data = currentNode in the source code. getData (); currentNode = currentNode. getNext (); nextIndex ++; return data ;}@ Overridepublic boolean hasNext () {return nextIndex! = Size ;}}}
Test> java
package com.chan;public class Test {public static void main(String[] args) {//Collection c = new ArrayList();Collection c = new LinkedList();for(int i=0; i<15; i++){c.add("test-"+i);}System.out.println(c.size());Iterator it = c.iterator();while(it.hasNext()) {Object o = it.next();System.out.print(o + " ");}}//15//test-0 test-1 test-2 test-3 test-4 test-5 test-6 test-7 test-8 test-9 test-10 test-11 test-12 test-13 test-14 }
The above code is tested.