Design Pattern - (Iterator|Cursor)

Card Puncher Data Processing

About

An iterator is an interface that can express sequences of unlimited size, such as the range of integers between 0 and Infinity.

It allow a user to loop over every element of a collection (container) while isolating the user from the internal structure of the container.

The Iterator pattern works for a collection of objects that you want to:

  • hand out through a common interface
  • in a particular order (with a Comparator behind the scenes)

The consuming code has only to call on method: the next() method.

Iterators date to the CLU programming language in 1974.

An iterator is behaviorally similar to a database cursor.

They're also complicated, stateful iterators, such as tree traversers.

Stream

Iterators are a useful abstraction of input streams that you can see as an infinite iteration.

Loop Implementation

Indexing

In procedural languages it is common to use the subscript operator and a loop counter to loop through all the elements in a sequence such as an array.

Iterator

The use of iterators may have some advantages against indexing:

  • Counting loops are not suitable to all data structures, in particular to data structures with no or slow random access, like lists or trees.
  • Iterators can provide a consistent way to iterate on data structures of all kinds, and therefore make the code more readable, reusable, and less sensitive to a change in the data structure.
  • An iterator can enforce additional restrictions on access, such as ensuring that elements can not be skipped or that a previously visited element can not be accessed a second time.
  • An iterator may allow the container object to be modified without invalidating the iterator. For instance, once an iterator has advanced beyond the first element it may be possible to insert additional elements into the beginning of the container with predictable results. With indexing this is problematic since the index numbers must change.

Trade-off

This is always a trade-off between security (iterators remain always valid) and efficiency. Most of the time, the added security in not worth the efficiency price to pay for it. Using an alternative container (for example a singly linked list instead of a vector) would be a better choice (globally more efficient) if the stability of the iterators is needed.

Documentation / Reference





Discover More
Card Puncher Data Processing
Android - Cursor

cursor implementation in Android. A cursor is what a content provider implementation will return in response of a query. Many iterators in Java implement the java/util/IteratorIterator interface, but...
Data System Architecture
Collection - Sequence (Ordered)

A sequence is an abstract collection: of non-unique element where order matters - ie (A,B) is not equal to (B,A) non-unique means that it allows duplicate member (the same element/value may occur...
Io Input Stream
I/O - Stream

A stream concept at the io level is a file (generally a text file) A stream is an abstract concept for files and io devices which can be read or written, or sometimes both. I/O devices can be interpreted...
Java Conceptuel Diagram
Java - Iterator Implementation

The iterator implementation in Java is just an inner class that implements the iterator interface. In order to be able to use it in a for loop construction, the iterable interface must be implemented....
Javascript - (Loop|Iterator)

This page is how to iterate in Javascript (ie the Iterator pattern in Javascript) To iterate / loop in Javascript, you can use the following syntax structure: the for statement the functional...
Javascript - Iterator

In Javascript, an iterator is an iterator An object has an iterator when it has: a property named Symbol.iterator that return an object that implement the iterator...
Card Puncher Data Processing
Language - Loop (For, While) - (Break, Continue)

Repeating a set of actions until a certain condition (predicate) fails is the job of programming loops; loops can take different forms, but they all satisfy this basic behavior. A loop includes: ...
Card Puncher Data Processing
Python - (Loop|Iteration)

See: Iterator Types
Data System Architecture
RDBMS - Cursor (Iterator)

A cursor represents data returned by a query. In computer science, a database cursor is a control structure that enables traversal over the records in a database. A cursor: contains tabular data ...
Card Puncher Data Processing
ReactiveX

is Functional_reactive_programmingFunctional reactive programming library (implemented in several languages) It composes asynchronous and event-based programs using observable asynchronous sequences...



Share this page:
Follow us:
Task Runner