Iterator pattern (Iterator)



GOF intent: To provide a way to sequentially access elements in an aggregate object, without exposing the object's internal representation (traverse each element in the collection)

 

Role:

Iterator:

ConcreteIterator (concrete iterator): holds a reference to the collection, knows the first one, and remembers where the collection is currently traversed, and knows how to find the next one, and knows when to end.

Aggregate (collection):

ConcreteAggregate: Concrete collections can be created and return an iterator.

 

Implementation points:

The iterator holds the collection reference and knows the first, current position, the next, the end of the traversal. A collection is created and an iterator is returned.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326846728&siteId=291194637