Design Pattern: Iterator Pattern)
What is the iterator mode?
Official explanation: to access the elements of an aggregate object sequentially without exposing its underlying implementation.
Sequential access to elements of a collection object without exposing its internal implementation
General explanation: given a set object and defining an associated Iterator (Iterator), The Iterator can access the internal elements of the Set object, by means of iteration, each element of the Set object can be accessed sequentially.
Why is the iterator mode used?
It allows the element structure inside the set to traverse the set when the client is transparent. Ensures encapsulation and transparency. Provides a unified method to access your Data Objects (sets), and you do not need to know the data object type. It is highly practical and widely used. It is highly scalable and easy to expand on its own.
How to Use the iterator mode?
The UML diagram is as follows:
Explanation of each component:
IteratZ restart? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcqO6s + large + sb31 + 67 + bG + tcS907/large + nP87yvus + jrLao0uXBy7yvus + large/ 2 bytes/K4w7yvus/kernel + yb6z/weight + weight/LT6xve21M/weight + 38zltfy0 + weight/A4MTasr/K/b7dveG5ubXExL + weight + signature + PC9wPg0KPHA + tbHE48/Signature + signature/Signature + PGJyIC8 + signature/zvK + 6z8Dgo6y/Signature + signature/Signature + rbUz/OjqLvy1d/Signature /LT6xvfEo8q9wLTKtc/W1eK49tDox/blend + sb30tS8sLyvus + 1 xLPpz/O907/blend + DQo8cHJlIGNsYXNzPQ = "brush: java; ">interface Iterator { T next(); boolean hasNext();}interface Collection { boolean add(T element); boolean remove(T element);}
2. Define the student object class. For the sake of conciseness, too many complex attributes are not defined.
public class Student { private String id; private String name; public Student(String id, String name) { this.id = id; this.name = name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; }}
3. Implement the aggregation class of specific student objects and define the specific iterator in the internal dependency.
Public class StudentList implements Collection
{List
ElementList = new ArrayList <> (); @ Override public void add (Student element) {elementList. add (element) ;}@ Override public boolean remove (Student element) {return elementList. remove (element);} public Iterator
CreateIterator () {return new StudentIterator (elementList);} // The specific Iterator class private class StudentIterator implements Iterator
{Private List
Students; private int position; public StudentIterator (List
Students) {this. students = students; this. position = 0 ;}@ Override public Student next () {if (hasNext () {return students. get (position ++) ;}return null ;}@ Override public boolean hasNext () {return position <students. size ();}}}
4. Use a client to call the mock test data.
Public class Client {public static void main (String [] args) {StudentList studentList = new StudentList (); studentList. add (new Student (001, James); studentList. add (new Student (002, Li Si); studentList. add (new Student (004, Zhao Wu); studentList. add (new Student (004, money 6); Iterator
Iterator = studentList. createIterator (); while (iterator. hasNext () {Student student = iterator. next (); System. out. println (student. getId () + student. getName ());}}}
Note: The abstract iterator and abstract set interface here only declare the simplest and most commonly used methods. Various methods can be added as needed in development, such as Java. the Iterator interface in Util is defined as follows:
The iterator can select different traversal methods based on the specific aggregation: Forward, reverse, and skip tables. The traversal may not be the entire object. Maybe you point to an attribute of the traversal object. In the above example, you point to the student's student ID, or the student ID is the most diligent, then you define an iteration method nextId to traverse the student's student ID.