The ITERATOR mode (ITERATOR), also known as Cursor, provides a method for sequentially accessing each element of an aggregate object without exposing the internal representation of the object, it belongs to the object behavior mode. The iterator mode separates the access and traversal of an aggregate object (usually a list) from the aggregate object and puts it into an iterator object. The iterator object knows how to traverse the list, in this way, not only can the implementation of aggregate objects be simplified (the traversal operation is handed over to the iterator and saved by the iterator), but the list can also be traversed in different ways.
I. Application scenarios
1. Access the content of an aggregate object without exposing its internal representation.
2. Supports multiple traversal of aggregate objects, such as forward traversal and reverse traversal. JDK iterators only implement forward traversal.
3. provides a unified interface for Traversing different aggregation structures. For different aggregate structures, you only need to switch the objects to be traversed without changing the code of the traversal process.
Ii. UML diagram
Iii. Java implementation
Package study. patterns. iterator; import java. util. ArrayList; import java. util. List;/*** iterator mode, which is generated to traverse the aggregation structure! * The iterator mode usually uses the factory method mode to instantiate an appropriate iterator subclass. * @ Author qbg */public class IteratorPattern {public static void main (String [] args) {List
List = new ArrayList
(); List. add ("Java programming ideas"); list. add ("design mode"); list. add ("programming craftsman"); list. add ("refactoring"); AbstractList
Books = new ConcreteList
(List); Iterator
Iterator = books. iterator (); System. out. println ("======= forward traversal =========="); while (iterator. hasNext () {System. out. print (iterator. next () + ",");} System. out. println ("\ n ======= reverse traversal =============="); while (iterator. hasPrevious () {System. out. print (iterator. previous () + ",") ;}}/ *** abstract aggregation class * @ param
*/Abstract class AbstractList
{Protected List
Elements = new ArrayList
(); Public role actlist (List
Eles) {this. elements = eles;} public void add (E e) {elements. add (e);} public void remove (E e) {elements. remove (e);} public List
GetAll () {return elements;}/*** declares the abstract Factory method for creating the Iterator object */public abstract Iterator
Iterator ();}/*** create an iterator based on this aggregation class * @ param
*/Class ConcreteList
Extends actlist
{Public ConcreteList (List
Eles) {super (eles) ;}@ Overridepublic Iterator
Iterator () {return new ConcreteIterator
(This) ;}}/*** traversal abstract interface, supporting wildcard */interface Iterator
{/*** Forward traversal to determine whether a successor node exists */public boolean hasNext ();/*** move the cursor down, returns the reference of the element crossed by the cursor */public E next ();/*** reverse traversal to determine whether there is a precursor node */public boolean hasPrevious (); /* ** move the cursor up, return the element reference of the cursor over */public E previous ();}/*** specific iterator for Traversing AbstractList
Set. * supports forward traversal and reverse traversal * @ param
*/Class ConcreteIterator
Implements Iterator
{Private registractlist
List; // private List of the set to be traversed
Elements; // The element private int cursor_forword in the Set; // forward traversal cursor, used to record the position private int cursor_backword in forward traversal; // The cursor in reverse traversal, used to record the position of reverse traversal public ConcreteIterator (AbstractList
List) {this. list = list; this. elements = this. list. getAll (); // get the set element this. cursor_forword = 0; // set the initial value of forward traversal cursor this. cursor_backword = elements. size ()-1; // set the initial value of the reverse traversal cursor} @ Overridepublic boolean hasNext () {return cursor_forword <elements. size () ;}@ Overridepublic E next () {E e = elements. get (cursor_forword); cursor_forword ++; return e ;}@ Overridepublic boolean hasPrevious () {return cursor_backword >=0 ;}@ Overridepublic E previous () {E e = elements. get (cursor_backword); cursor_backword --; return e ;}}
Running result:
========= Forward traversal =============== Java programming ideas, design patterns, programming skills, refactoring, ========== reverse traversal ============== refactoring, programming craftsmanship, design patterns, Java programming ideas,
Iv. Advantages and Disadvantages
Advantages:
1. Supports traversing an aggregation in different ways. To change the traversal mode, you only need to replace the original Instance with a different iterator instance.
2. The iterator simplifies the aggregation interface. With the iterator's traversal interface, aggregation itself does not need similar traversal interfaces.
3. Multiple traversal can be performed on the same aggregation. Each iterator maintains its own traversal status and does not affect each other.
Disadvantages:
1. It is difficult to grasp the design of the abstract iterator. If the preliminary design is poor, the later design will be greatly changed. For example, JDK iterators only support forward traversal. If you want to implement other traversal methods, you can only add helper classes.