Iterator pattern: Provides a way to access the individual elements of an aggregation object sequentially without exposing the internal representation of the image.
The iterator pattern should be the most familiar pattern, and the simplest proof is that I use the iterator provided by STL to traverse Vector or list data structure in the implementation of the combination mode, the element mode and the Observer mode.
The iterator pattern is also used to solve the traversal problem of an aggregate object, which encapsulates the traversal of the aggregation into a class, thus avoiding the possibility of exposing the internal representation of the aggregate object.
The motivation of the pattern:
(1) An aggregate object, such as a list or a set, should provide a way for others to access its elements without exposing its internal structure.
(2) There may be different ways to traverse the entire aggregation object for different needs, but we do not want to be flooded with different traversal operations in the abstract layer interface of the aggregation object.
(3) How to traverse an aggregation object, do not need to understand the internal structure of the aggregation object, but also can provide a variety of different traversal methods, this is the iterator model to solve the problem.
Structure Chart:
Example:
Namespace Iterator_designpattern {using System;
Using System.Collections;
Class Node {private string name;
public string Name {get {
} public Node (string s) {name = S;
} class NodeCollection {private ArrayList list = new ArrayList ();
private int nodemax = 0; Left as a student exercise-implement collection//functions to remove and edit entries also public void Addn Ode (Node N) {list.
ADD (n);
nodemax++;
Public Node getnode (int i) {return ((Node) list[i]);
public int Nodemax {get {return nodemax; }}/* * The iterator needs to understand and traverse the collection * It can do as way it plea
Ses-forward, Reverse, depth-first, */abstract class Iterator {abstract public Node Next (); Class Reverseiterator: Iterator {private nodecollection nodecollection;
private int currentindex;
Public Reverseiterator (NodeCollection c) {nodecollection = C; Currentindex = c.nodemax-1;
Array index starts at 0! }//Note:as The code stands, if the collection changes,//The iterator needs to be restarted override pub
Lic Node Next () {if (Currentindex = = 1) return null;
else return (Nodecollection.getnode (currentindex--));
}///<summary>///summary description for Client. </summary> public class Client {public static int Main (string[] args) {nodecollection c =
New NodeCollection ();
C.addnode (New Node ("a");
C.addnode (New Node ("second"));
C.addnode (New Node ("third"));
Now use iterator to traverse this reverseiterator i = new Reverseiterator (c);
The code below would work with any iterator type Node n; do {n = i.next ();
if (n!= null) Console.WriteLine ("{0}", n.name);
while (n!= null);
return 0;
}
}
}
Applicable scenario:
- Accesses the contents of an aggregated object without exposing its internal representation.
- Supports multiple traversal of aggregated objects.
- Provides a unified interface for traversing different aggregation structures (i.e., support for polymorphic iterations).