First, the introduction
The term iteration is definitely not unfamiliar to people who are familiar with Java. We often use the iterative interface provided by JDK for the traversal of Java collection:
Iterator it = list.iterator();
while(it.hasNext()){
//using “it.next();”do some businesss logic
}
This is a good example of an iterator pattern application.
II. Definition and structure
The iterator (iterator) pattern, also known as the cursor (Cursor) pattern. GOF is defined as providing a way to access the elements of a container (container) object without exposing the internal details of the object.
Visible from the definition, the iterator pattern is for the container. Obviously, the access to the container object necessarily involves the traversal algorithm. You can plug the Traversal method into a container object, or simply not provide a traversal algorithm to allow the person using the container to implement it. Both of these situations seem to solve the problem.
In the former case, however, the container has too much functionality, not only to maintain (add, delete, etc.) the elements of its own "container", but also to provide an interface to traverse itself, and it is not possible to iterate over the same container object at the same time because of the problem saved by traversal state. The second approach is easy, but it also exposes the inner details of the container.
And the appearance of the iterator pattern, has solved the above two kind of situation very well the malpractice. Let's look at the face of the iterator pattern first.
The iterator pattern consists of the following roles:
1) iterator role (iterator): The iterator role is responsible for defining interfaces that access and traverse elements.
2 The specific iterator role (concrete iterator): The specific iterator role implements the iterator interface and records the current position in the traversal.
3 container Role (Container): Container role is responsible for providing interfaces to create specific iterator roles.
4) Specific container role (concrete Container): Specific container role implementation of the interface to create a specific iterator role?? This specific iterator role is related to the structure of the container.
The class diagram of the iterator pattern is as follows:
As you can see from the structure, the iterator pattern adds an iterator role between the customer and the container. With the addition of the iterator role, it is possible to avoid the internal details of the container and make the design symbol a "single responsibility Principle".
Note that in the iterator pattern, the specific iterator role and the concrete container role are coupled together?? The traversal algorithm is closely related to the internal details of the container. In order to get the client program out of the dilemma of coupling with the specific iterator role, to avoid the change of the specific iterator role to the client program, the iterator pattern abstracts the specific iterator role, making the client program more general and reusable. This is called a polymorphic iteration.