2. iterator mode Overview
In software development, we often need to use aggregate objects to store a series of data. Aggregation objects have two responsibilities: one isStore Data2.Traverse data. From the perspective of dependency, the former is the basic responsibility of aggregation objects, while the latter is changeable and detachable. Therefore, we can separate the traversal data from the aggregate object and encapsulate it in an object called an iterator, the iterator is used to traverse the internal data of an aggregate object, which simplifies the design of the aggregate object and better meets the requirements of the "single responsibility principle.
The iterator mode is defined as follows:
Iterator pattern: provides a method to access an aggregate object without exposing its internal representation. Its alias is cursor ). The iterator mode is an object behavior mode. |
The iterator mode structure contains two layers: Aggregation and iterator. Considering the flexibility and scalability of the system,The factory method mode is applied in the iterator mode.The schema structure is as follows:
Figure 3 structure of the iterator Mode
The iterator mode structure includes the following roles:
● Iterator (Abstract iterator ):It defines interfaces for accessing and traversing elements and declares methods for Traversing data elements. For example, it is used to obtain the first () method of the first element and to access next () of the next element () the hasnext () method used to determine whether there is another element. It is used to obtain the currentitem () method of the current element. These methods will be implemented in a specific iterator.
● Concreteiterator (Specific iterator ):It implements the abstract iterator interface to traverse the aggregate objects, and records the current position in the aggregate object through the cursor in the specific iterator. In the specific implementation, A cursor is usually a non-negative integer that represents a position.
● Aggregate (Abstract aggregation class ):It is used to store and manage element objects and declares a createiterator () method to create an iterator object and act as the abstract iterator factory role.
● Concreteaggregate (specific aggregation class ):It implements the createiterator () method declared in the abstract aggregation class. This method returns a concreteiterator instance corresponding to the specific aggregation class.
In the iterator mode, an external iterator is provided to access and traverse aggregate objects. The iterator defines an interface to access the aggregate element, in addition, you can track the elements currently traversed to find out which elements have been traversed and which have not. The introduction of the iterator will simplify the operation on a complex aggregate object.
The following code is used to further analyze the structure of the iterator mode. The factory method mode is applied in the iterator mode. The abstract iterator corresponds to the abstract Product role, the specific iterator corresponds to the specific product role, and the abstract aggregation class corresponds to the abstract factory role, A specific aggregation class corresponds to a specific factory role.
The abstract iterator defines the method used to traverse elements stored in the aggregate object. The typical code is as follows:
Interface iterator {public void first (); // points the cursor to the first element public void next (); // points the cursor to the next element public Boolean hasnext (); // determine whether the next element public object currentitem () exists; // obtain the current element pointed to by the cursor}
The following code implements the data Traversal method declared by the abstract iterator in a specific iterator:
Class concreteiterator implements iterator {private concreteaggregate objects; // maintain a reference to a specific aggregate object to facilitate access to the data private int cursor stored in the aggregate object; // defines a cursor, used to record the current access location public concreteiterator (concreteaggregate objects) {This. objects = objects;} public void first (){......} public void next (){......} public Boolean hasnext (){......} public object currentitem (){......}}
It should be noted that the design of the abstract iterator interface is very important. On the one hand, we need to fully meet the requirements of various traversal operations, provide declarations for all kinds of traversal methods as much as possible, and on the other hand, it cannot contain too many methods, too many methods in the interface will cause trouble for the implementation of sub-classes. Therefore, considerThe abstract class is used to design the abstract iterator. In the abstract class, an empty default implementation is provided for each method..If you need to add a new traversal operation for the aggregate object in a specific iterator, you must modify the source code of the abstract iterator and the specific iterator, which violates the "open and closed principle ", therefore, it is necessary to consider all aspects during the design to avoid modifying the interface later.
The aggregation class is used to store data and create iterator objects. The simplest abstract aggregation class code is as follows:
interface Aggregate {Iterator createIterator();}
A specific aggregation class is a subclass of an abstract aggregation class. It stores data and implements the createiterator () method declared in the abstract aggregation class (), return a specific iterator object corresponding to the specific aggregation class. The Code is as follows:
class ConcreteAggregate implements Aggregate { ...... public Iterator createIterator() {return new ConcreteIterator(this); }......}
|
Thoughts Understand the dependency and association between a specific aggregation class and a specific iterator class in the iterator mode. |
|
【Author】 Liu WeiHttp://blog.csdn.net/lovelion]