﻿<?xml-stylesheet type="text/xsl" href="../../templates/doc.xsl"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html 
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:doc="http://www.lepus.org.uk/doc" 
	xmlns:classz="http://www.lepus.org.uk/classz" 
	xmlns:fopl="http://www.lepus.org.uk/fopl" 
	xml:lang="EN-GB" 
	xmlns:v="urn:schemas-microsoft-com:vml" 
	xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
	<link rel="stylesheet" type="text/css" href="../../templates/doc.css" />
	<title>Iterator pattern: The LePUS3 &amp; Class-Z companion to the GoF</title>
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta name="Author" content="Amnon H Eden" />
</head>


<body>


<h5><a href="./index.xml">The 'Gang of Four' Companion:</a></h5>

<p class="pagetitle">Iterator Design Pattern</p>

<h5><a href="./index.xml">Formal specification of design patterns in LePUS3 and Class-Z</a></h5>

<p><a href="companion.pdf" style="border:0px">
	<img alt="Print this document" src="../../site/print.gif" /></a></p>

<div class="abstract">

	<p>This page is part of the <a href="./index.xml">The 'Gang of Four' Companion</a> 
	dedicated to the formal specification in LePUS3 and Class-Z of patterns from the 'Gang of Four' catalogue [<a href="http://www.lepus.org.uk/ref/index.xml#bib">Gamma 
	et al 1995</a>].</p>

</div> <!-- Abstract-->

<doc:toc />

<h3>Links</h3>

<div class="abstract">

<ul>
		<li><a href="../legend/legend.xml">Legend: Key to LePUS3 and Class-Z Symbols</a></li>
		<li><a href="ref/refman/refman.xml">Reference Manual for LePUS3 &amp; Class-Z </a></li>
		<li><a href="faq.xml">Modelling Design Patterns: Frequently-Asked Questions</a></li>
	</ul>

	<p>Download:</p>
	
	<ul>
		<li><a href="../../spec/gof/lepus3/gof.vsd">The 'gang of four' patterns in LePUS3 (Visio 2003 format)</a></li>
		<li><a href="../../spec/gof/uml/gof.vsd">The 'gang of four' patterns in UML (Visio 2003 format)</a></li>
	</ul>
	
</div> <!-- Links -->


<h1>The Iterator design motif</h1>

<p>The informal description: Excerpts from [<a href="../index.xml#bib">Gamma et al. 1995</a>] (adapted for 
	this purpose):</p>

<p><strong>Intent</strong>: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.</p>
	
<p><strong>Structure</strong>: Original OMT diagram converted to UML (<a href="faq.xml#Why_did_you_convert_the_original_diagrams_to_UML_and_how?">Why 
	and How?</a>):</p>

<img alt="Iterator in UML" src="../../spec/gof/uml/Iterator.png" /> <br />

<p><strong>Participants</strong>: </p>
<ul>
	<li><b>Iterator</b>: defines an interface for accessing and traversing elements.</li> 
	<li><b>ConcreteIterator</b>: implements the Iterator interface and keeps track of the current position in the traversal of the aggregate.</li>
	<li><b>Aggregate</b>: defines an interface for creating an Iterator object.</li>
	<li><b>ConcreteAggregate</b>: implements the Iterator creation interface to return an instance of the proper ConcreteIterator.</li>
</ul>

<p><strong>Collaborations</strong>: A ConcreteIterator keeps track of the current object in the aggregate and can compute the succeeding object in the traversal.</p>

<h1>Formal specification</h1>
<p>See also:</p>
<ul>
		<li><a href="../legend/legend.xml">Legend: Key to LePUS3 and Class-Z Symbols</a></li>
		<li><a href="ref/refman/refman.xml">Reference Manual for LePUS3 &amp; Class-Z </a></li>
		<li><a href="../../spec/gof/classz/Iterator.xml">Iterator in XML</a></li>
		<li><a href="faq.xml">Modelling Design Patterns: Frequently-Asked Questions</a></li>
	</ul>

<table class="chartandschema">
<tr>
	<td>
	<img class="chart" id="Iterator" alt="Iterator in LePUS3" src="../../spec/gof/lepus3/Iterator.gif" /></td>
</tr>
<tr>
	<th>Iterator in LePUS3 (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td><doc:include value="../../spec/gof/classz/Iterator.xml" /></td>
</tr>
<tr>
	<th>Observer in Class-Z (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
</table>


<h1>Sample Implementations</h1>

<h2>Informal description: Collections and Iterators</h2>
<p>From [Gamma et. al 1995]:</p>

<blockquote>An aggregate object such as a list should give you a way to access its elements without exposing its internal structure. Moreover, you might want to traverse the list in different ways, depending on what you want to accomplish. But you probably don't want to bloat the List interface with operations for different traversals, even if you could anticipate the ones you will need. You might also need to have more than one traversal pending on the same list.</blockquote>

<blockquote>The Iterator pattern lets you do all this. The key idea in this pattern is to take the responsibility for access and traversal out of the list object and put it into an iterator object. The Iterator class defines an interface for accessing the list's elements. An iterator object is responsible for keeping track of the current element; that is, it knows which elements have been traversed already.</blockquote>
<blockquote>
	Notice that the iterator and the list are coupled, and the client must know 
	that it is a <em>list</em> that's traversed as opposed to some other 
	aggregate structure. Hence the client commits to a particular aggregate 
	structure. It would be better if we could change the aggregate class without 
	changing client code. We can do this by generalizing the iterator concept to 
	support <strong>polymorphic iteration</strong>.
</blockquote>
<blockquote>
	We define an AbstractList class that provides a common interface for 
	manipulating lists. Similarly, we need an abstract Iterator class that 
	defines a common iteration interface. Then we can define concrete Iterator 
	subclasses for the different list implementations. As a result, the 
	iteration mechanism becomes independent of concrete aggregate classes.
</blockquote>

<p>This design pattern, as informally described for lists above, can be seen 
implemented within the Java Collections framework. How this is done is presented 
below, and unless otherwise stated all classes reside within the package <code>
java.util</code>.</p>
<h2>Formal specification: Collections and Iterators</h2>

<p>Specified in LePUS3 and Class-Z (see <a href="../legend/legend.xml">legend</a>)</p>

<table class="chartandschema">
<tr>
	<td><img class="chart" alt="Iterator implementation" longdesc="Iterator implementation modelled in LePUS3 (1-dimensional)" src="../../spec/closed/IteratorImp/IteratorImp1DimHrc.gif" style="width: 345px; height: 270px" /></td>
</tr>
<tr>
	<th>Iterator implementation modelled in LePUS3 <br />
			using 1-dimensional hierarchy constants (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td>
	<img class="chart" alt="Iterator implementation" longdesc="Iterator implementation modelled in LePUS3 (0-dimensional)" src="../../spec/closed/IteratorImp/IteratorImp1DimCls.gif" style="width: 381px; height: 337px" /></td>
</tr>
<tr>
	<th>Iterator implementation modelled in LePUS3 <br />
			using 1-dimensional class constants (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td><img class="chart" alt="Iterator implementation" longdesc="Iterator implementation modelled in LePUS3 (0-dimensional)" src="../../spec/closed/IteratorImp/IteratorImp0DimCls.gif" style="width: 627px; height: 703px" /></td>
</tr>
<tr>
	<th>Iterator implementation modelled in LePUS3 <br />
			using 0-dimensional class (signature) constants (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
</table>


</body>
</html>
