Jump to content

Iterator pattern: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Vanderjoe (talk | contribs)
Added Overview section.
Added short description
Tags: Mobile edit Mobile app edit Android app edit App description add
 
(47 intermediate revisions by 27 users not shown)
Line 1: Line 1:
{{Short description|Software design pattern}}
In [[object-oriented programming]], the '''iterator pattern''' is a [[design pattern (computer science)|design pattern]] in which an [[iterator]] is used to traverse a [[container (data structure)|container]] and access the container's elements. The iterator pattern decouples [[algorithm]]s from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.
In [[object-oriented programming]], the '''iterator pattern''' is a [[design pattern (computer science)|design pattern]] in which an [[iterator]] is used to traverse a [[Collection (abstract data type)|container]] and access the container's elements. The iterator pattern decouples [[algorithm]]s from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.


For example, the hypothetical algorithm ''SearchForElement'' can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows ''SearchForElement'' to be used on any container that supports the required type of iterator.
For example, the hypothetical algorithm ''SearchForElement'' can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows ''SearchForElement'' to be used on any container that supports the required type of iterator.
{{TOC limit|3}}
{{TOC limit|3}}
<!-- READ NOTE BELOW BEFORE ADDING EXAMPLES

Wikipedia is not a list of examples. Do not add examples from your favorite programming language here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language extant. Feel free to add examples here: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Iterator
-->

==Definition==
[[Image:Iterator UML class diagram.svg|thumb|center|500px|The iterator pattern]]
The essence of the Iterator Pattern is to "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.".<ref>[[Design Patterns (book)|Gang Of Four]]</ref>


==Overview==
==Overview==
The Iterator
The Iterator
<ref name="GoF">{{cite book|author=Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides|title=Design Patterns: Elements of Reusable Object-Oriented Software|year=1994|publisher=Addison Wesley|isbn=0-201-63361-2|pages=257ff}}</ref>
<ref name="GoF">{{cite book|url=https://archive.org/details/designpatternsel00gamm/page/257|title=Design Patterns: Elements of Reusable Object-Oriented Software|author=Erich Gamma|last2=Richard Helm|last3=Ralph Johnson|last4=John Vlissides|publisher=Addison Wesley|year=1994|isbn=0-201-63361-2|pages=[https://archive.org/details/designpatternsel00gamm/page/257 257ff]|url-access=registration}}</ref>
design pattern is one of the twenty-three well-known
design pattern is one of the 23 well-known
''[[Design Patterns|GoF design patterns]]''
''[[Design Patterns|"Gang of Four" design patterns]]''
that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.
that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.


<big>What problems can the Iterator design pattern solve?</big>
===What problems can the Iterator design pattern solve?===
<ref>{{cite web|title=The Iterator design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=b04&ugr=proble|website=w3sDesign.com|accessdate=2017-08-12}}</ref>
<ref>{{cite web|title=The Iterator design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=b04&ugr=proble|website=w3sDesign.com|access-date=2017-08-12}}</ref>
* The elements of an aggregate object should be accessed and traversed without exposing the representation (data structures) of the aggregate.
* The elements of an aggregate object should be accessed and traversed without exposing its representation (data structures).
* New traversal operations should be defined for the elements of an aggregate object without changing the aggregate.
* New traversal operations should be defined for an aggregate object without changing its interface.


Defining the access and traversal operations within the aggregate is inflexible becausee it commits the aggregate to particular
Defining access and traversal operations in the aggregate interface is inflexible because it commits the aggregate to particular access and traversal operations and makes it impossible to add new operations
later without having to change the aggregate interface.
access and traversal operations and makes it impossible to add new operations
later without having to change the aggregate.


<big>What solution does the Iterator design pattern describe?</big>
===What solution does the Iterator design pattern describe?===


* Define a separate (iterator) object that encapsulates accessing and traversing an aggregate.
* Define a separate (iterator) object that encapsulates accessing and traversing an aggregate object.
* Clients use an iterator object to access and traverse an aggregate without knowing the representation (data structures) of the aggregate.
* Clients use an iterator to access and traverse an aggregate without knowing its representation (data structures).


Different iterator objects can be used to access and traverse an aggregate in different ways.
Different iterators can be used to access and traverse an aggregate in different ways.
<br>New access and traversal operations can be defined independently by defining new iterators.


See also the UML class and sequence diagram below.
See also the UML class and sequence diagram below.


==Definition==
== Language-specific implementation ==
The essence of the Iterator Pattern is to "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.".<ref>[[Design Patterns (book)|Gang Of Four]]</ref>
{{main|Iterator}}
Some languages standardize syntax. C++ and Python are notable examples.


=== C# ===
== Structure ==
=== UML class and sequence diagram ===
[[.NET Framework]] has special interfaces that support a simple iteration: <code>System.Collections.IEnumerator</code> over a non-generic collection and <code>System.Collections.Generic.IEnumerator<T></code> over a generic collection.
[[File:w3sDesign Iterator Design Pattern UML.jpg|frame|none|A sample UML class and sequence diagram for the Iterator design pattern.<ref>{{cite web|title=The Iterator design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=b04&ugr=struct|website=w3sDesign.com|access-date=2017-08-12}}</ref>]]
In the above [[UML]] [[class diagram]], the <code>Client</code> class refers (1) to the <code>Aggregate</code> interface for creating an <code>Iterator</code> object (<code>createIterator()</code>) and (2) to the <code>Iterator</code> interface for traversing an <code>Aggregate</code> object (<code>next(),hasNext()</code>).
The <code>Iterator1</code> class implements the <code>Iterator</code> interface by accessing the <code>Aggregate1</code> class.


The [[UML]] [[sequence diagram]]
[[C Sharp (programming language)|C#]] statement <code>foreach</code> is designed to easily iterate through the collection that implements <code>System.Collections.IEnumerator</code> and/or <code>System.Collections.Generic.IEnumerator<T></code> interface. Since C# v2, <code>foreach</code> is also able to iterate through types that implement <code>System.Collections.Generic.IEnumerable<T></code> and <code>System.Collections.Generic.IEnumerator<T></code> <ref>https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/foreach-in</ref>
shows the run-time interactions: The <code>Client</code> object calls <code>createIterator()</code> on an <code>Aggregate1</code> object, which creates an <code>Iterator1</code> object and returns it
to the <code>Client</code>.
The <code>Client</code> uses then <code>Iterator1</code> to traverse the elements of the <code>Aggregate1</code> object.


=== UML class diagram ===
Example of using <code>foreach</code> statement:
[[Image:Iterator UML class diagram.svg|thumb|center|500px|The iterator pattern]]

<source lang=CSharp>
var primes = new List<int>{ 2, 3, 5, 7, 11, 13, 17, 19};
long m = 1;
foreach (var p in primes)
m *= p;
</source>


== Example ==
{{main|Iterator}}
Some languages standardize syntax. C++ and Python are notable examples.
<!-- READ NOTE BELOW BEFORE ADDING EXAMPLES
Wikipedia is not a list of examples. Do not add examples from your favorite programming language here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language extant. Feel free to add examples here: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Iterator
-->
=== C++ ===
=== C++ ===
[[C++]] implements iterators with the semantics of [[pointer (computer programming)|pointer]]s in that language. In C++, a class can overload all of the pointer operations, so an iterator can be implemented that acts more or less like a pointer, complete with dereference, increment, and decrement. This has the advantage that C++ algorithms such as <code>std::sort</code> can immediately be applied to plain old memory buffers, and that there is no new syntax to learn. However, it requires an "end" iterator to test for equality, rather than allowing an iterator to know that it has reached the end. In C++ language, we say that an iterator models the iterator [[concept (generic programming)|concept]].
[[C++]] implements iterators with the semantics of [[pointer (computer programming)|pointer]]s in that language. In C++, a class can overload all of the pointer operations, so an iterator can be implemented that acts more or less like a pointer, complete with dereference, increment, and decrement. This has the advantage that C++ algorithms such as <code>std::sort</code> can immediately be applied to plain old memory buffers, and that there is no new syntax to learn. However, it requires an "end" iterator to test for equality, rather than allowing an iterator to know that it has reached the end. In C++ language, we say that an iterator models the iterator [[concept (generic programming)|concept]].


This C++11 implementation is based on chapter "Generalizing vector yet again".<ref>{{cite book |author=Bjarne Stroustrup |title=Programming: Principles and Practice using C++ |edition=2 |publisher=Addison Wesley |year=2014 |isbn=978-0-321-99278-9 |pages=729 ff.}}</ref>
=== Java ===
Java has the {{Javadoc:SE|java/util|Iterator}} interface.


<syntaxhighlight lang="c++">
As of Java 5, objects implementing the {{Javadoc:SE|java/lang|Iterable}} interface, which returns an <code>Iterator</code> from its only method, can be traversed using Java's [[foreach loop]] syntax. The {{Javadoc:SE|java/util|Collection}} interface from the [[Java collections framework]] extends <code>Iterable</code>.
#include <iostream>

#include <stdexcept>
=== JavaScript ===
#include <initializer_list>

[[JavaScript]], as part of [[ECMAScript#6th Edition|ECMAScript 6]], supports the iterator pattern with any object that provides a <code>next()</code> method, which returns an object with two specific properties: <code>done</code> and <code>value</code>. Here's an example that shows a reverse array iterator:
class Vector {

public:
<source lang=JavaScript>
using iterator = double*;
function reverseArrayIterator(array) {
iterator begin() { return elem; }
var index = array.length - 1;
return {
iterator end() { return elem + sz; }
next: () =>
Vector(std::initializer_list<double> lst) :elem(nullptr), sz(0) {
index >= 0 ?
sz = lst.size();
{value: array[index--], done: false} :
{done: true}
elem = new double[sz];
double* p = elem;
for (auto i = lst.begin(); i != lst.end(); ++i, ++p) {
*p = *i;
}
}
}
}
~Vector() { delete[] elem; }
int size() const { return sz; }
double& operator[](int n) {
if (n < 0 || n >= sz) throw std::out_of_range("Vector::operator[]");
return elem[n];
}
Vector(const Vector&) = delete; // rule of three
Vector& operator=(const Vector&) = delete;
private:
double* elem;
int sz;
};


int main() {
const it = reverseArrayIterator(['three', 'two', 'one']);
Vector v = {1.1*1.1, 2.2*2.2};
console.log(it.next().value); //-> 'one'
console.log(it.next().value); //-> 'two'
for (const auto& x : v) {
console.log(it.next().value); //-> 'three'
std::cout << x << '\n';
console.log(`Are you done? ${it.next().done}`); //-> true
</source>

Most of the time, though, what you want is to provide Iterator<ref>{{cite web |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators |title=Iterators and generators |accessdate=18 March 2016}}</ref> semantics on objects so that they can be iterated automatically via <code>for...of</code> loops. Some of JavaScript's built-in types such as <code>Array</code>, <code>Map</code>, or <code>Set</code> already define their own iteration behavior. You can achieve the same effect by defining an object's meta <code>@@iterator</code> method, also referred to by <code>Symbol.iterator</code>. This creates an Iterable object.

Here's an example of a range function that generates a list of values starting from <code>start</code> to <code>end</code>, exclusive, using a regular <code>for</code> loop to generate the numbers:

<source lang=JavaScript>
function range(start, end) {
return {
[Symbol.iterator]() { //#A
return this;
},
next() {
if(start < end) {
return { value: start++, done:false }; //#B
}
return { done: true, value:end }; //#B
}
}
}
for (auto i = v.begin(); i != v.end(); ++i) {
std::cout << *i << '\n';
}
for (auto i = 0; i <= v.size(); ++i) {
std::cout << v[i] << '\n';
}
}
}
</syntaxhighlight>


The program output is
for(number of range(1, 5)) {
console.log(number); //-> 1, 2, 3, 4
}
</source>

The iteration mechanism of built-in types, like strings, can also be manipulated:

<source lang=JavaScript>
let iter = ['I', 't', 'e', 'r', 'a', 't', 'o', 'r'][Symbol.iterator]();
iter.next().value; //-> I
iter.next().value; //-> t
</source>

=== PHP ===

[[PHP]] supports the iterator pattern via the Iterator interface, as part of the standard distribution.<ref>{{cite web |url=http://www.php.net/manual/en/class.iterator.php |title=PHP: Iterator |accessdate=23 June 2013}}</ref> Objects that implement the interface can be iterated over with the <code>foreach</code> language construct.

Example of patterns using PHP:

<source lang=PHP>
<?php

// BookIterator.php

namespace DesignPatterns;

class BookIterator implements \Iterator
{
private $i_position = 0;
private $booksCollection;

public function __construct(BookCollection $booksCollection)
{
$this->booksCollection = $booksCollection;
}

public function current()
{
return $this->booksCollection->getTitle($this->i_position);
}

public function key()
{
return $this->i_position;
}

public function next()
{
$this->i_position++;
}

public function rewind()
{
$this->i_position = 0;
}

public function valid()
{
return !is_null($this->booksCollection->getTitle($this->i_position));
}
}
</source>
<source lang=PHP>
<?php

// BookCollection.php

namespace DesignPatterns;

class BookCollection implements \IteratorAggregate
{
private $a_titles = array();

public function getIterator()
{
return new BookIterator($this);
}

public function addTitle($string)
{
$this->a_titles[] = $string;
}

public function getTitle($key)
{
if (isset($this->a_titles[$key])) {
return $this->a_titles[$key];
}
return null;
}

public function is_empty()
{
return empty($a_titles);
}
}
</source>
<source lang=PHP>
<?php

// index.php

require 'vendor/autoload.php';
use DesignPatterns\BookCollection;

$booksCollection = new BookCollection();
$booksCollection->addTitle('Design Patterns');
$booksCollection->addTitle('PHP7 is the best');
$booksCollection->addTitle('Laravel Rules');
$booksCollection->addTitle('DHH Rules');

foreach($booksCollection as $book){
var_dump($book);
}
</source>

==== OUTPUT ====
string(15) "Design Patterns"
string(16) "PHP7 is the best"
string(13) "Laravel Rules"
string(9) "DHH Rules"

=== Python ===
[[Python (programming language)|Python]] prescribes a syntax for iterators as part of the language itself, so that language keywords such as <code>for</code> work with what Python calls sequences. A sequence has an <code>__iter__()</code> method that returns an iterator object. The "iterator protocol" requires <code>next()</code> return the next element or raise a <code>StopIteration</code> exception upon reaching the end of the sequence. Iterators also provide an <code>__iter__()</code> method returning themselves so that they can also be iterated over e.g., using a <code>for</code> loop. [[Generator (computer programming)#Python|Generators]] are available since 2.2.


<syntaxhighlight lang="c++">
In Python 3, <code>next()</code> was renamed <code>__next__()</code>.<ref>{{cite web |url=https://docs.python.org/library/stdtypes.html |title=Python v2.7.1 documentation: The Python Standard Library: 5. Built-in Types |accessdate=2 May 2011}}</ref>
1.21
4.84
1.21
4.84
1.21
4.84
terminate called after throwing an instance of 'std::out_of_range'
what(): Vector::operator[]
</syntaxhighlight>


== See also ==
== See also ==
Line 245: Line 132:
* [http://us3.php.net/manual/en/language.oop5.iterations.php Object iteration] in PHP
* [http://us3.php.net/manual/en/language.oop5.iterations.php Object iteration] in PHP
* [http://www.dofactory.com/Patterns/PatternIterator.aspx Iterator Pattern] in C#
* [http://www.dofactory.com/Patterns/PatternIterator.aspx Iterator Pattern] in C#
* [http://www.lepus.org.uk/ref/companion/Iterator.xml Iterator pattern in UML and in LePUS3 (a formal modelling language)]
* [https://web.archive.org/web/20160303172550/http://www.lepus.org.uk/ref/companion/Iterator.xml Iterator pattern in UML and in LePUS3 (a formal modelling language)]
* [http://sourcemaking.com/design_patterns/iterator SourceMaking tutorial]
* [http://sourcemaking.com/design_patterns/iterator SourceMaking tutorial]
* [http://patterns.pl/iterator.html Design Patterns implementation examples tutorial]
* [https://web.archive.org/web/20150520003129/http://www.patterns.pl/iterator.html Design Patterns implementation examples tutorial]
* [http://c2.com/cgi/wiki?IteratorPattern Iterator Pattern]
* [http://c2.com/cgi/wiki?IteratorPattern Iterator Pattern]


Line 259: Line 146:
[[Category:Articles with example PHP code]]
[[Category:Articles with example PHP code]]
[[Category:Iteration in programming]]
[[Category:Iteration in programming]]
[[Category:Software design patterns]]

Latest revision as of 19:37, 16 September 2024

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

For example, the hypothetical algorithm SearchForElement can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows SearchForElement to be used on any container that supports the required type of iterator.

Overview

[edit]

The Iterator [1] design pattern is one of the 23 well-known "Gang of Four" design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

What problems can the Iterator design pattern solve?

[edit]

[2]

  • The elements of an aggregate object should be accessed and traversed without exposing its representation (data structures).
  • New traversal operations should be defined for an aggregate object without changing its interface.

Defining access and traversal operations in the aggregate interface is inflexible because it commits the aggregate to particular access and traversal operations and makes it impossible to add new operations later without having to change the aggregate interface.

What solution does the Iterator design pattern describe?

[edit]
  • Define a separate (iterator) object that encapsulates accessing and traversing an aggregate object.
  • Clients use an iterator to access and traverse an aggregate without knowing its representation (data structures).

Different iterators can be used to access and traverse an aggregate in different ways.
New access and traversal operations can be defined independently by defining new iterators.

See also the UML class and sequence diagram below.

Definition

[edit]

The essence of the Iterator Pattern is to "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.".[3]

Structure

[edit]

UML class and sequence diagram

[edit]
A sample UML class and sequence diagram for the Iterator design pattern.[4]

In the above UML class diagram, the Client class refers (1) to the Aggregate interface for creating an Iterator object (createIterator()) and (2) to the Iterator interface for traversing an Aggregate object (next(),hasNext()). The Iterator1 class implements the Iterator interface by accessing the Aggregate1 class.

The UML sequence diagram shows the run-time interactions: The Client object calls createIterator() on an Aggregate1 object, which creates an Iterator1 object and returns it to the Client. The Client uses then Iterator1 to traverse the elements of the Aggregate1 object.

UML class diagram

[edit]
The iterator pattern

Example

[edit]

Some languages standardize syntax. C++ and Python are notable examples.

C++

[edit]

C++ implements iterators with the semantics of pointers in that language. In C++, a class can overload all of the pointer operations, so an iterator can be implemented that acts more or less like a pointer, complete with dereference, increment, and decrement. This has the advantage that C++ algorithms such as std::sort can immediately be applied to plain old memory buffers, and that there is no new syntax to learn. However, it requires an "end" iterator to test for equality, rather than allowing an iterator to know that it has reached the end. In C++ language, we say that an iterator models the iterator concept.

This C++11 implementation is based on chapter "Generalizing vector yet again".[5]

#include <iostream>
#include <stdexcept>
#include <initializer_list>
		
class Vector {
public:
  using iterator = double*;
  iterator begin() { return elem; }
  iterator end() { return elem + sz; }
  
  Vector(std::initializer_list<double> lst) :elem(nullptr), sz(0) {
    sz = lst.size();
    elem = new double[sz];
    double* p = elem;
    for (auto i = lst.begin(); i != lst.end(); ++i, ++p) {
      *p = *i;
    }
  }  
  ~Vector() { delete[] elem; }
  int size() const { return sz; }
  double& operator[](int n) {
    if (n < 0 || n >= sz) throw std::out_of_range("Vector::operator[]");
    return elem[n];
  }
  Vector(const Vector&) = delete; // rule of three
  Vector& operator=(const Vector&) = delete;
private:
  double* elem;
  int sz;
};

int main() {
  Vector v = {1.1*1.1, 2.2*2.2};
  
  for (const auto& x : v) {
    std::cout << x << '\n';
  }
  for (auto i = v.begin(); i != v.end(); ++i) {
    std::cout << *i << '\n';
  }
  for (auto i = 0; i <= v.size(); ++i) {
    std::cout << v[i] << '\n';
  } 
}

The program output is

1.21
4.84
1.21
4.84
1.21
4.84
terminate called after throwing an instance of 'std::out_of_range'
  what():  Vector::operator[]

See also

[edit]

References

[edit]
  1. ^ Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 257ff. ISBN 0-201-63361-2.
  2. ^ "The Iterator design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-12.
  3. ^ Gang Of Four
  4. ^ "The Iterator design pattern - Structure and Collaboration". w3sDesign.com. Retrieved 2017-08-12.
  5. ^ Bjarne Stroustrup (2014). Programming: Principles and Practice using C++ (2 ed.). Addison Wesley. pp. 729 ff. ISBN 978-0-321-99278-9.
[edit]