Iterator (owner of design)
In software Genius, the iterator is a Patron of design (design pattern) behavioral.
A iterator is an object which makes it possible to traverse all the elements contained in another object, generally a container (Liste, tree, etc). A synonym of iterator is Curseur, in particular in the context of the databases.
Description
A iterator resembles a Pointeur having two primitives primarily: to reach the pointed element in progress (in the container), and to move to point towards the following element. In addition, it is necessary to be able to create a iterator pointing on the first element; as to determine constantly if the iterator exhausted the totality of the elements of the container. Various implementations can also offer additional behaviors.
The goal of a iterator is to allow his user to traverse the container, i.e. to reach all its elements sequentially to apply a treatment to them, while isolating the user from the internal structure of the container, potentially complex. Thus, the container can store the elements in the way which he wants, while allowing the user to treat it like a simple list. Generally the iterator is designed at the same time as the class-container that it will have to traverse, and it will be the container itself which will create and distribute the itérateurs to reach its elements.
Differences with the indexing
In the procedural languages one often uses an index in simple a Boucle, to reach all the elements sequentially, in particular of a table. Though this approach remains possible in Programmation object for certain containers, the use of the iterator S.A. certain advantages:
- a simple meter in a loop is not adapted to all the structures of data, in particular
- those which do not have access method to an unspecified element
- those whose access to an unspecified element is very slow (it is the case of the chained lists and the tree S).
- the itérateurs provide a coherent means to reiterate on all kinds of structures of data, thus making the code customer more readable, reusable, and robust even in the event of change in the organization of the structure of data.
- a iterator can establish additional restrictions on the access to the elements, for example to prevent that an element “is jumped”, or that the same element is visited twice.
- a iterator can in certain cases allow that the container is modified, without being invalidated for as much. For example, after a iterator positioned behind the first element, it is possible to insert other elements into the beginning of the container with foreseeable results. With an index there would be more problems, because the value of the index should it also be modified consequently.
Important: consult well the documentation of your iterator to know in what a case it is invalidated or not!
The possibility for a container of seeing itself modified during an iteration was essential like necessary in the modern Programmation object, where the relations between objects and the effect of certain operations can become a headache. By using such a iterator " robuste" , these nuisances are saved to us.
Implicit Itérateurs
Object-oriented languages like Perl and Python provide an average “intern” to reiterate on the elements of a container without introducing a iterator explicitly. That is often established by a structure of control for-each , as in the following examples:
The language C++ also has the function template std:: for_each () which allows similar implicit iterations, but always requires to provide itérateurs objects in parameters of entry.
| Random links: | Catherine of Bologna | The Impure ones | Neal Hefti | Vallo Torinese | Chaffaut-Lagremuse | Leon_Roppolo |