Behavioral mode: Iterator Mode
1. Intention
Provides a method to access each element in a set object sequentially without exposing the internal representation of the object.
2. Alias
Cursor)
3. Motivation
An aggregate object, such as a list, should provide a way for others to access its elements without exposing its internal structure. The most common Iterator mode in databases.
4. Applicability
The Iterator mode is used in the following cases:
Access the content of an aggregate object without exposing its internal representation. Supports multiple traversal of aggregate objects. Provides a unified interface (supporting multi-state iteration) for Traversing different aggregation structures ).
5. Structure
As the name suggests, the iterator mode is used to access objects in the aggregation sequentially. Generally, collection is very common. If you are familiar with collection classes, it is very easy to understand this mode. This sentence contains two meanings: first, the object to be traversed, that is, the clustering object, and second, the iterator object, which is used to traverse and access the clustering object. Let's look at the relationship diagram:
This idea is exactly the same as what we usually use. MyCollection defines some operations of the set. MyIterator defines a series of iterative operations and holds Collection instances. Let's take a look at the implementation code.
6. Sample Code
Implementation Code:
Two interfaces:
Public interface Collection {public Iterator iterator ();/* get Set element */public Object get (int I);/* get Set size */public int size ();} public interface Iterator {// forward public Object previous (); // move public Object next (); public boolean hasNext (); // obtain the first element public Object first ();}
Two implementations:
public class MyCollection implements Collection { public String string[] = {A,B,C,D,E}; @Override public Iterator iterator() { return new MyIterator(this); } @Override public Object get(int i) { return string[i]; } @Override public int size() { return string.length; }}public class MyIterator implements Iterator { private Collection collection; private int pos = -1; public MyIterator(Collection collection){ this.collection = collection; } @Override public Object previous() { if(pos > 0){ pos--; } return collection.get(pos); } @Override public Object next() { if(pos
Test class:
public class Test {
public static void main(String[] args) { Collection collection = new MyCollection(); Iterator it = collection.iterator(); while(it.hasNext()){ System.out.println(it.next()); }}
}
“`
Output: A B C D E
Here we seem to have simulated the process of a collection class. Is it nice? In fact, all the classes in JDK are also these basic things. We can add some design patterns and some optimizations together. As long as we have learned and mastered these things, we can also write our own collection classes and even frameworks!
7. Related Modes
Composite: The iterator is often applied to a recursive structure that conforms to this pattern. Factory Method: the multi-state iterator uses Factory Method to instantiate an appropriate iterator subclass. Memento: often used together with the iterator mode. The iterator can use a memento to capture the status of an iteration. The iterator stores memento internally.